How To Solve Firebaseerror: Expected First Argument To Collection() To Be A Collectionreference, A Documentreference Or Firebasefirestore Problem?
I am trying to set up Firebase with next.js. I am getting this error in the console. FirebaseError: Expected first argument to collection() to be a CollectionReference, a Document
Solution 1:
Using getFirestore from lite library will not work with onSnapshot. You are importing getFirestore from lite version:
import { getFirestore } from'firebase/firestore/lite'Change the import to:
import { getFirestore } from'firebase/firestore'From the documentation,
The
onSnapshotmethod andDocumentChange,SnapshotListenerOptions,SnapshotMetadata,SnapshotOptionsandUnsubscribeobjects are not included inliteversion.
Another reason for this error to show up could be passing invalid first argument to collection() or doc() functions. They both take a Firestore instance as first argument.
// Ensure that "db" is defined and initializedconst db = getFirestore();
// console.log(db);const colRef = collection(db, "collection_name");
Solution 2:
Adding to @Dharmaraj, if you are using firebase react hooks, use the reverse.
Instead of
import { getFirestore } from'firebase/firestore'Use
import { getFirestore } from'firebase/firestore/lite'
Post a Comment for "How To Solve Firebaseerror: Expected First Argument To Collection() To Be A Collectionreference, A Documentreference Or Firebasefirestore Problem?"