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

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-24 03:03:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!