Shaheryar Ajmal
Shaheryar Ajmal

Reputation: 39

Multiple conditions on firestore queries reactjs

How do you use multiple conditionals on queries on the firestore database when using reactjs. I have the code for a single conditional but not multiple. I have found the documentation poor when using firestore with reactjs.

Single Conditional:

const qSnap = await getDocs(query(collectedInfoRef, where("email", "==", sendFields['user_email'])));

Upvotes: 0

Views: 208

Answers (1)

Nicholas Tower
Nicholas Tower

Reputation: 85161

query can take multiple arguments. Just add a comma, and then your next condition, and repeat if necessary

const qSnap = await getDocs(
  query(
    collectedInfoRef,
    where("email", "==", sendFields["user_email"]),
    where("foo", "==", "bar"),
    limit(3),
  )
);

Note that for some queries, you may need to create an index for it to work. If you try to run a query that needs an index, check your dev console and you'll see a message which has a link that you can follow to create the index

Upvotes: 1

Related Questions