NextJS Server not able to use firestore
I have some functions I need to run on both the front and backend of a nextjs server. I am trying to use firebase on the server side to deal with some sensitive data, but I am not able to make a docref to the firestore collection doc. I keep getting the following error: but I have printed out the offending value and it is a valid. Below is the code:
//page/api/firestore
import { doc } from "firebase/firestore";
import { db } from "lib/firestore";
export default async function handler(req, res) {
try {
const docRef = doc(db, "bots", "access_code");
} catch (e) {
console.log(e);
}
res.status(200).json({});
}
and the db value is defined below:
//lib/firestore
export default initializeApp({
apiKey: "**********",
authDomain: "**********",
projectId: "**********",
storageBucket: "**********",
messagingSenderId: "**********",
appId: "**********",
});
export const db = getFirestore(app);
I am doing the exact some thing on the frontend and it works fine ðŸ˜
Solution 1:
I figured it out! NextJS uses NodeJS in the api directory, but I was using the firebase Web 9 SDK for both the front and backend. Once I switched to the Node SDK for the api files everything worked fine!!!