Usama Abdul Razzaq
Usama Abdul Razzaq

Reputation: 803

OrderBy and where query is not working at the same time in firebase

I am have almost this structure of data in my firestore and

1 2

I am fetching data using two queries orderBy and where but there two are not working and giving me error

3

the piece of code i am using is this

 useEffect(() => {
    db.collection("accounts")
      .doc("[email protected]")
      .collection("glasses")
      .where("stock", ">", 0)
      .orderBy("id", "desc")
      .onSnapshot((snapshot) =>
        setGlasses(snapshot.docs.map((doc) => doc.data()))
      );
  }, []);

if i use only single query orderBy() or where() then the query works but the combination of there query dont work together.

Solution of the problem i want is to get the data whose stock is greater then zero and also order the data according to id

Upvotes: 0

Views: 791

Answers (1)

samthecodingman
samthecodingman

Reputation: 26171

You need to use .orderBy("stock").orderBy("id", "desc") to fix that query.

However this won't give the results you want. Instead you'll need to add a boolean field "hasStock" that has the same meaning as what you are querying.

Upvotes: 1

Related Questions