Error (firestore/failed-precondition) when chaining two Where function calls via Firestore.
Issue
I have this code below, what I was trying to accomplish was to chain a where on a collection query via firestore. Trying to chain two wheres where one is an '==' while the other is an '>=' causes (firestore/failed-precondition) issue.
Each document on that collection looks like below:
reservationDocument: {
dateTime: TimeStamp,
deleted: boolean,
duration: number,
}
While my code to test the query was below:
import firebase from 'react-native-firebase';
const query = () => {
firebase
.firestore()
.collection('reservations')
.where('deleted', '==', false)
.where('dateTime', '>=', new Date())
.get()
.then(data => console.log('DATA', data))
.catch(err => console.error(err));
};
Environment
- Application Target Platform:
Both
-
Development Operating System: OS: macOS High Sierra 10.13.5
-
Build Tools: Xcode: Xcode 9.4 Build version 9F1027a Android Studio: 3.1 AI-173.4819257
Node: 8.11.2
Yarn: 1.7.0
npm: 6.2.0
Watchman: 4.9.0
4. React Native version:
react: 16.3.1 => 16.3.1
react-native: 0.55.4 => 0.55.4
5. React Native Firebase Version:
"react-native-firebase": "^4.3.8",
6. Firebase Module:
firestore,
- Are you using
typescript? no
Potentialy related https://github.com/invertase/react-native-firebase/issues/1439
Doesn't worked if combine both conditions or either one.
.where("date", ">=", date1MonthAgo.getTime())
.where("date", "<=", Date.now())
I've gone through every Date based querying scenario I could think of plus the ones linked in the other issues and I've been unable to reproduce any errors on either platform;
You can view all the tests I added for this here: https://github.com/invertase/react-native-firebase/commit/6bc668c8ab9e1e3cf3a990348a9ac9f4596f6bd8
This may have been an issue with the Native SDKs or an earlier release but everything appears to be fine now.
I'll go ahead and close this issue now. I'd suggest if anyone has an issue like this again submit a PR with a test that fails - the groundwork for this has been done in that commit.
Loving react-native-firebase and the support we provide? Please consider supporting us with any of the below:
- 👉 Back financially via Open Collective
- 👉 Follow
React Native FirebaseandInvertaseon Twitter - 👉 Star this repo on GitHub ⭐️
This solution worked for me
https://github.com/invertase/react-native-firebase/issues/815#issuecomment-465741365
This usually means that you either:
- Need to change your firestore rules or most likely,
- You need an index on the where field. You can create it manually in the firebase console or run this in a terminal and you will find a link to create it automagically:
adb logcat *:W
The link will be something like this:
Caused by: io.grpc.StatusException: FAILED_PRECONDITION: The query requires an index. That index is currently building and cannot be used yet. See its status here: https://console.firebase.google.com/v1/r/project/...