Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

FirebaseError: Expected first argument to collection() to be a CollectionReference

ErrorFirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore

Short answer

In the modular Firebase SDK, collection() is a function whose first argument must be the Firestore instance from getFirestore(app). The error means you passed something else: undefined, a missing import, or an object from the older namespaced API. Pass the db created by getFirestore and keep one API style.

  • Applies to Firebase JS SDK 11, Next.js 15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log the value you pass as the first argument. It should be a Firestore object, not undefined and not a plain config.

    console.log(db);
    const ref = collection(db, "users");
  2. 2Match what you see, then jump to its fix

    1. 1console.log(db) prints undefined, or prints the firebaseConfig or app objectdb is undefined or is not the Firestore instanceFix
    2. 2Code has both firebase.firestore() and collection(db, ...), or db came from firebase/compatThe namespaced and modular APIs are mixed upFix

What does the error look like?

  • FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore

Cause 1 of 2

db is undefined or is not the Firestore instance

1Check

Check how db is exported and imported. A wrong import path, or exporting app instead of db, gives undefined or the wrong object.

grep -rn "getFirestore\|import .*db" src/ lib/ 2>/dev/null

2Fix

Create db once with getFirestore(app), export it, and import that same name wherever collection() is called. The Firebase quickstart passes db as the first argument every time.

// lib/firebase.js
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";

const app = initializeApp(firebaseConfig);
export const db = getFirestore(app);

// elsewhere
import { db } from "@/lib/firebase";
import { collection, getDocs } from "firebase/firestore";
const snap = await getDocs(collection(db, "users"));

3Verify

The call returns a snapshot and the error is gone.

console.log(snap.size);

Confirmed by firebase.google.com

Did this fix it?

Cause 2 of 2

The namespaced and modular APIs are mixed up

1Check

The two styles differ: namespaced code calls db.collection("x"), modular code calls collection(db, "x"). Look for compat imports next to modular calls.

grep -rn "firebase/compat\|firebase.firestore()" src/

2Fix

Move the file to one style. In the modular style use getFirestore and free functions. Compat objects only work with compat calls, per the upgrade guide.

// namespaced (compat)
const db = firebase.firestore();
db.collection("cities").get();

// modular
import { getFirestore, collection, getDocs } from "firebase/firestore";
const db = getFirestore(app);
await getDocs(collection(db, "cities"));

3Verify

Run the page again; no FirebaseError is thrown.

npm run dev

Confirmed by firebase.google.com

Did this fix it?

How do I stop it coming back?

  • Export db from one module and import it everywhere; never rebuild it inline.
  • Do not mix firebase/compat imports with modular calls in the same file.
  • In Next.js, keep the Firebase init file free of server-only values so it imports cleanly on both sides.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

Google sign-in popup blocked by Cross-Origin-Opener-Policy

The page loses its reference to the sign-in popup window, because the opener policy severs it

If your site sends Cross-Origin-Opener-Policy: same-origin, a sign-in popup from another origin lands in a separate browsing context group, so your page cannot reach it.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.