I am trying to initialize the Admin SDK in a NextJS app. I have initialized the app as so:
import { getFirestore } from '@firebase/firestore';
import admin from 'firebase-admin';
if (!admin.apps.length) {
admin.initializeApp({
credential: admin.credential.cert({
project_id: process.env.NEXT_PUBLIC_PROJECT_ID,
private_key: process.env.FIREBASE_PRIVATE_KEY,
client_email: process.env.FIREBASE_CLIENT_EMAIL
})
});
}
export default getFirestore();
and then try to use the database in the following code snippet:
import { getDocs, collection } from '@firebase/firestore';
import db from '@/lib/firebase-admin';
export default async (_, res) => {
let sites = [];
const querySnapshot = await getDocs(collection(db, 'sites'));
querySnapshot.forEach((doc) => {
sites.push(doc.id, ...doc);
});
res.status(200).json(sites);
};
But I am presented with the following error: Next JS Firebase Error
FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore
I am using the Web V9 (module) components of Firebase, and have yet to figure out why I am getting this error. Any help would be greatly appreciated!