admin管理员组

文章数量:1302888

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 bine the where and the orderBy, something I have done before with other Firestore collections, it fails.

Here is a sample record:

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 bine the where and the orderBy, something I have done before with other Firestore collections, it fails.

Here is a sample record:

Share Improve this question edited Dec 11, 2017 at 1:25 Frank van Puffelen 600k85 gold badges889 silver badges859 bronze badges asked Dec 11, 2017 at 1:15 AdamGAdamG 2,6303 gold badges25 silver badges39 bronze badges 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. – Frank van Puffelen 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./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. – Frank van Puffelen Commented Dec 11, 2017 at 2:04
Add a ment  | 

2 Answers 2

Reset to default 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.

I encountered this same issue, and Frank van Puffelen's ment fixed it for me. You need to create a posite 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.

本文标签: