Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FAILED_PRECONDITION when using where and orderBy together #515

Closed
mackenziemance opened this issue Oct 15, 2017 · 35 comments
Closed

FAILED_PRECONDITION when using where and orderBy together #515

mackenziemance opened this issue Oct 15, 2017 · 35 comments

Comments

@mackenziemance
Copy link

mackenziemance commented Oct 15, 2017

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

@mackenziemance
Copy link
Author

mackenziemance commented Oct 16, 2017

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

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

@chrisbianca
Copy link
Contributor

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

@mackenziemance
Copy link
Author

mackenziemance commented Oct 16, 2017

@chrisbianca

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

@chrisbianca
Copy link
Contributor

@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.

@mackenziemance
Copy link
Author

mackenziemance commented Oct 17, 2017

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

@mikesurowiec
Copy link

@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

@urajat
Copy link
Contributor

urajat commented Oct 23, 2017

@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.

@rtman
Copy link

rtman commented Oct 23, 2017

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?

@chrisbianca
Copy link
Contributor

chrisbianca commented Oct 23, 2017

@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

@rtman
Copy link

rtman commented Oct 23, 2017

Oops, didn't try that yet, will do

@rtman
Copy link

rtman commented Oct 24, 2017

@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!

@chrisbianca
Copy link
Contributor

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

@agersoncgps
Copy link

agersoncgps commented Nov 26, 2017

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?

@brunoro
Copy link
Contributor

brunoro commented Dec 6, 2017

@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>

@noahtallen
Copy link
Contributor

noahtallen commented Aug 6, 2018

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.

@tiendn
Copy link

tiendn commented Oct 3, 2018

@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

@cmmartin
Copy link

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

@luco
Copy link

luco commented Apr 26, 2019

@cmmartin Same here

@ahmetardal
Copy link

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.

@tkow
Copy link

tkow commented Jun 24, 2019

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?

@leron8
Copy link

leron8 commented Aug 9, 2019

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"

@technoplato
Copy link

@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?

@leron8
Copy link

leron8 commented Aug 29, 2019

@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.

@technoplato
Copy 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

@phatmann
Copy link

@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.

@Ehesp
Copy link
Member

Ehesp commented Sep 11, 2019

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

@phatmann
Copy link

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

@aliakgul
Copy link

aliakgul commented Sep 17, 2019

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)

@yash-atreya
Copy link

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 ??

@mikehardy
Copy link
Collaborator

@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

@yash-atreya
Copy link

@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!

@leron8
Copy link

leron8 commented Nov 15, 2019

@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); }

@FitFingers
Copy link

@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!

@yash-atreya
Copy link

@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

@suhaillilac
Copy link

ReactNativeJS

08-12 12:36:32.040 13843 15800 W Firestore:

adb logcat -s Firestore

worked for me, Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests