Firebase - Unable to sort and filter data at the same time

Refresh

April 2019

Views

61 time

1

I am using Firestore as my database. I wish to filter my data by using one variable and then order it chronologically using a second variable.

My code to try and achieve this is the following:

firebase.firestore().collection('supervisorRequests')
  .where("supervisor", "==", this.props.currentUID)
  .orderBy("postDate", "desc")

For the code above, I am getting the following output:

Firestore: Operation was rejected because the system is not in a state required for the operation's execution.(firestore/failed-precondition)

If I remove either .orderBy or .where from the query, the problem disappears.

What is the cause of the problem? Why can't I use both "filters"? How can I obtain the data the way I requested it and avoid having to sort it manually on my end?

1 answers

0

Sometime i have this kind of error with your situation : doing orderby in a field and doing where in another field.

An attempt of solution that work for me (using android studio) is to create an index. To know the relative link to this specific index, in the terminal, i use this command :

adb logcat

It will you show a log. You can cancel this command with "crtl + c" and then scroll up a bit where you will see the exact link to use. You copy this link and paste it in the browser. But you must authenticate first in your firebase console. And then a modal will appear to ask you if you want to create this index : you accept.

Or if you want you can manually create this index directly in firebase console. Manage indexes in Cloud Firestore