Skip to content Skip to sidebar Skip to footer

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 onSnapshot method and DocumentChange, SnapshotListenerOptions, SnapshotMetadata, SnapshotOptions and Unsubscribe objects are not included in lite version.


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 initialized
const 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?"