SShah
SShah

Reputation: 263

Cloud FireStore : NativeFirebaseError: [firestore/failed-precondition] The query requires an index.?

I am trying to get data from firestore but gives me error. I had two condition one condition is of userid and other condition is of get last 7 days data.

getReportData = (callBack) => {

var query = firestore().collection("booking")
query = query.where("docId", "==", firebase.auth().currentUser.uid)
const currentTime = new Date();
var d = new Date();
d.setDate(d.getDate() - 8);

query = query.where("createdAt", ">=", d);

query.get().then((doc) => {
  const list = [];
  doc.forEach(function (docV) {
    list.push(docV.data());
  });
  console.log("data" , list);
  console.log("data length" , list.length);
}).catch(function (error) {
  console.log('booking error', error);

})

}

It gives me error

NativeFirebaseError: [firestore/failed-precondition] The query requires an index. You can create it here

Upvotes: 1

Views: 795

Answers (2)

Leffa
Leffa

Reputation: 494

Go to control panel in firebase and find index tab

Firebase index panel

but you not need to think about this, because the React Native Firebase when shows the error, he give a link like a:

[firestore/failed-precondition] The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project...

When you click and open the panel the firebase will create a index for you.

Upvotes: 0

Simon
Simon

Reputation: 6462

Everything is said in the error, you need an index to execute this query.

Firebase helps a lot here, because it even provides you a link to directly create the index. You only have to follow the link to create the correct index and then your query will work ;)

Upvotes: 1

Related Questions