AdamG
AdamG

Reputation: 2630

Firebase Firestore: orderBy combined with where causes error "Operation was rejected"

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

Upvotes: 9

Views: 4303

Answers (2)

Caphson
Caphson

Reputation: 256

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.

Upvotes: 7

Connor M
Connor M

Reputation: 341

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.

Upvotes: 1

Related Questions