问题
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:
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/47745241/firebase-firestore-orderby-combined-with-where-causes-error-operation-was-reje