2

to limit the fetch to FirestoreDB I tried to use the recent namespace to getDocs().

const firebaseColRef = collection(db, "collection")

This is what I tried:

const firebaseColRef = collection(db, "collection").limit(x)

And this:

const getDB = async () => {
    const data = await getDocs(firebaseColRef).limit(x);
    setFilmDB(data.docs.map((doc) => ({ ...doc.data(), id: doc.id })));
  }

In both cases the console prompts:

Uncaught (in promise) TypeError: firebase_firestore__WEBPACK_IMPORTED_MODULE_1__.collection(...).limit is not a function

I went through several documentations, tutorials and articels and was not able to find any other namespace then the above mentioned. And tried any possible append of .limit(x) to get any other result than the console prompt. Nothing worked.

What am I missing ?

Can anyone point me in the right direction ?

Would be very appreciated. Thanks, Anthony

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84

1 Answers1

2

Almost every Firebase method is a top-level function in Modular SDK and can be imported directly from the relevant SDKs. Try refactoring the code as shown below.

import { collection, getDocs, query, where, limit, orderBy } from "firebase/firestore";

const getDB = async () => {
  const colRef = collection(db, "collection");

  // Keep adding query clauses in query()
  // like chaining them in name-spaced V8 version
  const q = query(colRef, where("field", "==", "value"), limit(1));

  const data = await getDocs(q);
  console.log(data.docs.map((doc) => ({ ...doc.data(), id: doc.id })))
}

The name-spaced version of the same query for reference:

const q = db.collection("col").where("field", "==", "value").limit(1);

You can find list of all functions in the new Firestore SDK in the documentation.

Also checkout:

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • :) that worked ! tried to upvote your answer but I seem to be a novice and need more reputations. So, just a plain THANK YOU ! – Anthony Zornig Mar 18 '22 at 19:19
  • 1
    Hey, if the answer helped you, consider accepting it (this can be done with low reputation) so users with similar issues can see this question as answered and use the solution – Kevin Quinzel Mar 23 '22 at 17:29