Home > Net >  how to query firestore v9 data in react js
how to query firestore v9 data in react js

Time:11-07

How to perform a query with conditional where() clause using firebase and cloud-firestore v9?

  useEffect(
    () =>
      onSnapshot(
        collection(db, "Article"),
        where("category", "array-contains", "Apple"),
        (snapshot) =>
          setArticles(
            snapshot.docs.map((doc) => ({
              ...doc.data(),
              id: doc.id,
            }))
          )
      ),
    []
  );

CodePudding user response:

You're missing a query call:

onSnapshot(
  query(
    collection(db, "Article"),
    where("category", "array-contains", "Apple"),
  )
  (snapshot) =>
  ...

Also see the Firebase documentation on queries, which has examples for both the v9 SDK and older/other SDKs.

  • Related