Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine orderBy and where clauses in Firestore

I am trying to run a simple query on Firestore, using both where and orderBy. The query does not work, with no error messages. After some research, I found out that I might need to build index in Firestore. I did that, but once again, this query does not work.

Is there some solutions? Not beeing able to run where + orderBy would be a huge weakness.

Here is the code:

var firestore = firebase.firestore();
firestore.collection(collec_name).where("approved", "==", 1).orderBy('signin_date', 'desc').limit(3).get().then(snapshot => {
      snapshot.forEach(doc => {
...
}
})


1 Answers

The query does not work, with no error messages

You're not getting any error messages because you're not actually checking for any errors. Your code should have a catch block to see what went wrong:

var firestore = firebase.firestore();
firestore.collection(collec_name)
    .where("approved", "==",1)
    .orderBy('signin_date', 'desc')
    .limit(3)
    .get()
.then(snapshot => {
    snapshot.forEach(doc => {
    }
})
.catch(error => {
    console.error(error)
})

If your query does actually need an index, the error message printed here will give you a link to click that creates the index needed to satisfy this query.

like image 151
Doug Stevenson Avatar answered Oct 28 '25 20:10

Doug Stevenson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!