React-native-firebase: FAILED_PRECONDITION when using where and orderBy together

Created on 16 Oct 2017  路  34Comments  路  Source: invertase/react-native-firebase

When querying a collection with .where().orderBy() I get the FAILED_PRECONDITION error code. I've tried orderBy before where and querying different collections all instances produce this err for me. Both methods work on there own. Can someone reproduce ?

Dev Tools output

Error: Firestore: Operation was rejected because the system is not in a state required for the operation`s execution. (firestore/failed-precondition).
at createErrorFromErrorData (NativeModules.js:117)
at NativeModules.js:80
at MessageQueue.__invokeCallback (MessageQueue.js:354)
at MessageQueue.js:129
at MessageQueue.__guard (MessageQueue.js:269)
at MessageQueue.invokeCallbackAndReturnFlushedQueue (MessageQueue.js:128)
at debuggerWorker.js:72

Environment

macOS: High Sierra
Xcode: 9.0
react-native: 0.48.4
react-native-firebase: 3.0.3

Most helpful comment

@mackenziemance I had the same problem on Android. When I did adb logcat, it showed that there was an index missing for this particular query - it even gave me the Firestore URL to use to create that index. Might be the same case here too.

All 34 comments

btw I'm using equality and not range operator when catching error

screen shot 2017-10-15 at 11 34 04 pm

@mackenziemance Can you add the snippet of code that you're running? Please include the .where and .orderBy conditions,

@chrisbianca

initialEvents() {
   firebase.firestore().collection('events').where('ended', '==', false).orderBy('sp', 'desc').get()
   .then(this.handleEvents)
   .catch((err)=> console.log(err));
}

@mackenziemance Have you tried the same query on Android to see what it does? I think this is a Firestore issue rather than an RNFirebase issue as we're just passing back the error we receive.

@chrisbianca No but I will. I figured that was probably the case, just thought I鈥檇 see if anyone else had experienced this issue

@mackenziemance I was also running into issues with orderBy() on iOS; as far as I could tell adding orderBy('createdAt', 'desc') to my query prevented the onSnapshot handler from being called when new data was present. the where() condition didn't seem to affect my particular case

@mackenziemance I had the same problem on Android. When I did adb logcat, it showed that there was an index missing for this particular query - it even gave me the Firestore URL to use to create that index. Might be the same case here too.

Hey I'm getting the same thing on Android:

        this.inventoriesRef = firebase.firestore().collection('inventories');
        this.inventoriesRef
            .where(`tags.${tag}`, '==', true)
            .orderBy('itemNameKey')
            .get()
            .then((querySnapshot) => {
})

Is there any update on this?

@rtman Have you tried @urajat's suggestion? As explained above, this looks to be an underlying issue with the Firebase SDKs and not an RNFirebase issue as we're merely returning the error that the SDK gives us

Oops, didn't try that yet, will do

@chrisbianca
I see the url in logcat, it is requesting I make an composite index for this. I can confirm it works after creating the index!

Ok great. I'm going to close this as it's definitely a firebase SDK issue rather than RNFirebase.

I am getting this error in the chrome console for iOS, but it does not print a URL to create the proper index. Where would I find that URL?

@agersoncgps the URL won't show up on react-native log-android, as it has the RNFSCollectionReference tag instead of the ReactNativeJS one. Run adb logcat and you should get the full error.

The line you're looking for should look like this:

RNFSCollectionReference: com.google.firebase.firestore.FirebaseFirestoreException: FAILED_PRECONDITION: The query requires an index. You can create it here: <url>

Is there a particular reason that the URL for creating an index doesn't get passed to the error message on the js side?

Edit:
I wasn't able to find the URL in the iOS logs, but I did find it on Android with the command adb logcat -s RNFSCollectionReference and then refreshed the app so that the code got run again, and it showed right up.

@noahtallen , I have tried the command adb logcat -s RNFSCollectionReference, but it didn't work.
screen shot 2018-10-03 at 3 46 54 pm

It would be awesome if there was a way to get these errors (which include the index creation url) into the javascript console. Currently, you have to view them in XCode for iOS or using adb logcat for Android

@cmmartin Same here

In Xcode, click Target->Edit Scheme.
Under Arguments tab there's Environment Variables. Uncheck OS_ACTIVITY_MODE entry if there's any. Now you can see all system logs in Xcode logs pane. Search for The query requires an index, you will find the error message and the index creation link as well.

this.inventoriesRef = firebase.firestore().collection('inventories');
        this.inventoriesRef
            .where(`tags.${tag}`, '==', true)
            .orderBy('itemNameKey')
            .get()
            .then((querySnapshot) => {
})

I found this query needs indexes each tags, but according to https://firebase.google.com/docs/firestore/query-data/index-overview?hl=en#index_limitations, the compound index limit is 200. So, if we have these keys are id, it cannot be used to order records.Curiously, js-sdk can read with startAfter inputed to last documentReference, but [email protected] cannot. Is there any solution for this?

For android users, you can use this command to view the url to create the composed index, since that's the error being presented when you try to use a where and order by in the same query:

adb logcat | grep -F "adb shell ps | grep [your.package.name] | tr -s [:space:] ' ' | cut -d' ' -f2"

@ardalahmet I tried that and still the Firebase Index Creation URL is still not showing in the XCode log. I am currently trying Project Clean + Installing on a different simulator. Will see if there's any luck.

Any other ideas though? What version of react-native-firebase do you have?

@technoplato , have you tried adding catch blocks in your query's promise result and debug via react-native debugger? I'm thinking that it should also give you the create index link.

@leron8 Good suggestion. I didn't do that, but I did fix the error by creating an Index for my collectionGroup query. This article helped a ton: https://firebase.googleblog.com/2019/06/understanding-collection-group-queries.html

@tkow There is a solution for queries that fail when startAt is used: instead of passing in a document snapshot to startAt, use a field property instead. Per the docs both should work, but they don't.

@phatmann which version are you using? v6 has loads of fixes for Firestore queries, passing a document ref in was one.

@Ehesp I am using v6. This same query worked fine with v5.

I reached the auto-index-creator link by react-native log-android and the link was similar to this:
FAILED_PRECONDITION: The query requires an index. You can create it here: https://console.firebase.google.com/project/YOURAPPID/database/firestore/indexes?create_composite=INDEXID
I couldn't create an index automatically because the link redirected me to an empty firebase console. My solution was not to use CTRL + LeftClick, but copy and paste the link into my browser.

In addition, if you will create index for map fields create like this:

myMap: {//suppose that this is your field
myElement
}

create index as: myMap.`myElement`` (with one ' et the end)

For android users, you can use this command to view the url to create the composed index, since that's the error being presented when you try to use a where and order by in the same query:

adb logcat | grep -F "adb shell ps | grep [your.package.name] | tr -s [:space:] ' ' | cut -d' ' -f2"

What should I replace '[:space:]' with ??

@yash1511 if I am not mistaken that is a 'tr' command expression syntax way of saying literally "a space character", you don't need to replace it http://www.softpanorama.org/Tools/tr.shtml

@yash1511 if I am not mistaken that is a 'tr' command expression syntax way of saying literally "a space character", you don't need to replace it http://www.softpanorama.org/Tools/tr.shtml

Okay, thank you!

@yash1511 have you tried this?:
https://github.com/invertase/react-native-firebase/issues/515#issuecomment-526225788

The code would be something like this:
(on the query's object promise)
.catch((error)=>{ reject(error); });

and also use the query's error callback:
,(error)=>{ console.log(error); reject(error); }

@mackenziemance I had the same problem on Android. When I did adb logcat, it showed that there was an index missing for this particular query - it even gave me the Firestore URL to use to create that index. Might be the same case here too.

I had the exact same issue and this fixed it. I didn't get any error but I found it in adb logcat. Thanks!

@yash1511 have you tried this?:
https://github.com/invertase/react-native-firebase/issues/515#issuecomment-526225788

The code would be something like this:
(on the query's object promise)
.catch((error)=>{ reject(error); });

and also use the query's error callback:
,(error)=>{ console.log(error); reject(error); }

Thanks, I fixed it

Was this page helpful?
0 / 5 - 0 ratings