9

I am looking at the Firebase Cloud Firestore documentation for orderBy. When I try to execute this

var facultyQuery = facultyRef.where("department", "==", "Core Teacher").orderBy('bb_last_name', 'desc');

I get the error:

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

Both of these simpler cases work just fine:

var facultyQuery = facultyRef.orderBy('bb_last_name', 'asc');
var facultyQuery = facultyRef.where("department", "==", "Core Teacher");

But when I combine the where and the orderBy, something I have done before with other Firestore collections, it fails.

Here is a sample record: enter image description here

3
  • Did you create the necessary index on department and bb_last_name? You should get a better error message than this if you forgot, but I'd like to make sure. Commented Dec 11, 2017 at 1:28
  • I did not. The documentation states that "...queries that only use equality clauses don't need additional indexes" so I did not think I needed one. firebase.google.com/docs/firestore/query-data/indexing
    – AdamG
    Commented Dec 11, 2017 at 1:39
  • It's the additional orderBy that makes me think it might. But it would usually give a very clear message about that. Commented Dec 11, 2017 at 2:04

2 Answers 2

7

Tip for anyone that needs it,

If you've forgotten to create an index and are getting the above error, run adb logcat and then attempt to load the data again - it usually gives you a URL link which will very kindly create the required index for you.

1

I encountered this same issue, and Frank van Puffelen's comment fixed it for me. You need to create a composite index for "department" and "bb_last_name". Since "department" uses the equality operator, it doesn't matter whether it is ascending or descending in your index.

1
  • Searched all over and didn't find this. I also had to match the ascending/descending on the index to match my query (which makes sense). Thank you.
    – pinguinos
    Commented Jun 28, 2019 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.