2
I’m making a query in my database in the firestore, but when I put two where nested, I get the following error:
Error getting documents Error: Firestore: Operation was rejected because the system is not in a state required for the operation`s execution. (firestore/failed-precondition).
When I consult with only one of the Where, the query works.
Code:
firebase
  .firestore()
  .collection('users')
  .doc(userId)
  .collection('cars')
  .where('available', '==', true)
  .where('createdAt', '<=', new Date(moment().format('MMMM DD, YYYY')))
  .get()
  .then((snapshot) => {
      const cars = [];
      snapshot.forEach((doc) => {
        cars.push(Object.assign({}, { id: doc.id }, doc.data()));
      });
      this.setState({ cars, loading: false });
  })
  .catch((err) => {
      console.log('Error getting documents', err);
      this.setState({ loading: false });
  });
						
Why a composite index should be created?
– rbz
Firestore requires the creation of compound indices when different fields are used, if it was a range in the createAt field with <= and then >= does not need, but if you try to use 2 different fields in Where as this case 'available' and 'createAr' you need to create the Indice right on the console.
– Rodolfo Patane