Nextjs 14 app API and am using firebase for chat in my app
I have nextjs 14 app and am using firebase for chat in my app. Create API for for i get below errors when i build my app. Path (app/api/(chat)/users)
./node_modules/@firebase/database-compat/dist/index.standalone.js:13:1
Module not found: Can't resolve 'tls'
https://nextjs.org/docs/messages/module-not-found
Import trace for requested module:
./node_modules/firebase-admin/lib/app/firebase-namespace.js
./node_modules/firebase-admin/lib/default-namespace.js
./node_modules/firebase-admin/lib/index.js
./app/api/(chat)/firebase/config.js
./app/api/(chat)/users/route.js
./node_modules/next/dist/build/webpack/loaders/next-edge-app-route-loader/index.js?absolutePagePath=private-next-app-dir%2Fapi%2F(chat)%2Fusers%2Froute.js&page=%2Fapi%2F(chat)%2Fusers%2Froute&appDirLoader=bmV4dC1hcHAtbG9hZGVyP25hbWU9YXBwJTJGYXBpJTJGKGNoYXQpJTJGdXNlcnMlMkZyb3V0ZSZwYWdlPSUyRmFwaSUyRihjaGF0KSUyRnVzZXJzJTJGcm91dGUmcGFnZVBhdGg9cHJpdmF0ZS1uZXh0LWFwcC1kaXIlMkZhcGklMkYoY2hhdCklMkZ1c2VycyUyRnJvdXRlLmpzJmFwcERpcj0lMkZob21lJTJGYmVhc3QlMkZEZXNrdG9wJTJGbXQtdjEtY2xvdWRmbGFyZS1wb2MlMkZhcHAmYXBwUGF0aHM9JTJGYXBpJTJGKGNoYXQpJTJGdXNlcnMlMkZyb3V0ZSZwYWdlRXh0ZW5zaW9ucz10c3gmcGFnZUV4dGVuc2lvbnM9dHMmcGFnZUV4dGVuc2lvbnM9anN4JnBhZ2VFeHRlbnNpb25zPWpzJmJhc2VQYXRoPSZhc3NldFByZWZpeD0mbmV4dENvbmZpZ091dHB1dD0mcHJlZmVycmVkUmVnaW9uPSZtaWRkbGV3YXJlQ29uZmlnPWUzMCUzRCE%3D&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D!
./node_modules/@google-cloud/firestore/build/src/index.js:19:1
Module not found: Can't resolve 'stream'
https://nextjs.org/docs/messages/module-not-found
./node_modules/@firebase/database-compat/dist/index.standalone.js:13:1
Module not found: Can't resolve 'tls'
https://nextjs.org/docs/messages/module-not-found
Import trace for requested module:
./node_modules/firebase-admin/lib/app/firebase-namespace.js
./node_modules/firebase-admin/lib/default-namespace.js
./node_modules/firebase-admin/lib/index.js
./app/api/(chat)/firebase/config.js
./app/api/(chat)/users/route.js
./node_modules/next/dist/build/webpack/loaders/next-edge-app-route-loader/index.js?absolutePagePath=private-next-app-dir%2Fapi%2F(chat)%2Fusers%2Froute.js&page=%2Fapi%2F(chat)%2Fusers%2Froute&appDirLoader=bmV4dC1hcHAtbG9hZGVyP25hbWU9YXBwJTJGYXBpJTJGKGNoYXQpJTJGdXNlcnMlMkZyb3V0ZSZwYWdlPSUyRmFwaSUyRihjaGF0KSUyRnVzZXJzJTJGcm91dGUmcGFnZVBhdGg9cHJpdmF0ZS1uZXh0LWFwcC1kaXIlMkZhcGklMkYoY2hhdCklMkZ1c2VycyUyRnJvdXRlLmpzJmFwcERpcj0lMkZob21lJTJGYmVhc3QlMkZEZXNrdG9wJTJGbXQtdjEtY2xvdWRmbGFyZS1wb2MlMkZhcHAmYXBwUGF0aHM9JTJGYXBpJTJGKGNoYXQpJTJGdXNlcnMlMkZyb3V0ZSZwYWdlRXh0ZW5zaW9ucz10c3gmcGFnZUV4dGVuc2lvbnM9dHMmcGFnZUV4dGVuc2lvbnM9anN4JnBhZ2VFeHRlbnNpb25zPWpzJmJhc2VQYXRoPSZhc3NldFByZWZpeD0mbmV4dENvbmZpZ091dHB1dD0mcHJlZmVycmVkUmVnaW9uPSZtaWRkbGV3YXJlQ29uZmlnPWUzMCUzRCE%3D&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D!
./node_modules/@google-cloud/firestore/build/src/index.js:19:1
Module not found: Can't resolve 'stream'
https://nextjs.org/docs/messages/module-not-found
1 Reply
here is code
// api endpoint to update user online status
import { db_api } from '@/app/api/(chat)/firebase/config';
import { isUserAuthenticated } from '@/app/api/(chat)/utils/utils';
export const runtime = 'edge';
export async function PUT(request) {
if (!isUserAuthenticated(request)) {
return new Response('Unauthorized', { status: 401 });
}
const formData = await request.formData();
const username = formData.get('username');
const userBadge = formData.get('userBadge'); // Ensure this value is correctly obtained
const online = formData.get('online');
// Basic validation
if (!username || online === null) {
return new Response('Bad Request', { status: 400 });
}
// console.log(`Updating user: ${username} with badge: ${userBadge} and online status: ${online}`);
// Get a reference to the user document in Firestore
const userRef = db_api.collection('users').doc(username);
try {
// Update the user's online status, last_changed timestamp, and userBadge
// Firestore will automatically create the document if it doesn't exist
await userRef.set(
{
username,
online: online === 'true' ? true : false,
last_changed: Date.now(),
...(userBadge ? { userBadge } : {}), // Conditionally add userBadge if it's provided
},
{ merge: true },
);
return new Response('User updated', { status: 200 });
} catch (error) {
console.error('Error updating user:', error);
// Provide more detailed error information in the response
return new Response(`Error updating user: ${error.message}`, {
status: 500,
statusText: 'Internal Server Error',
});
}
}
// api endpoint to update user online status
import { db_api } from '@/app/api/(chat)/firebase/config';
import { isUserAuthenticated } from '@/app/api/(chat)/utils/utils';
export const runtime = 'edge';
export async function PUT(request) {
if (!isUserAuthenticated(request)) {
return new Response('Unauthorized', { status: 401 });
}
const formData = await request.formData();
const username = formData.get('username');
const userBadge = formData.get('userBadge'); // Ensure this value is correctly obtained
const online = formData.get('online');
// Basic validation
if (!username || online === null) {
return new Response('Bad Request', { status: 400 });
}
// console.log(`Updating user: ${username} with badge: ${userBadge} and online status: ${online}`);
// Get a reference to the user document in Firestore
const userRef = db_api.collection('users').doc(username);
try {
// Update the user's online status, last_changed timestamp, and userBadge
// Firestore will automatically create the document if it doesn't exist
await userRef.set(
{
username,
online: online === 'true' ? true : false,
last_changed: Date.now(),
...(userBadge ? { userBadge } : {}), // Conditionally add userBadge if it's provided
},
{ merge: true },
);
return new Response('User updated', { status: 200 });
} catch (error) {
console.error('Error updating user:', error);
// Provide more detailed error information in the response
return new Response(`Error updating user: ${error.message}`, {
status: 500,
statusText: 'Internal Server Error',
});
}
}