0

My Firestore DB structure enter image description here

In NodeJS, I am trying to get all cases with caseStatus "active" and apptTimeInEpochSeconds within a range.

I am trying to achieve it using the below code:

const casesRef = db.collection("rch" + "/hospital/cases");
  const query = casesRef
    .where("patientDetails.caseStatus", "==", keys.active)
    .where("patientDetails.apptTimeInEpochSeconds", ">", 1601461817)
  .where("patientDetails.apptTimeInEpochSeconds", "<", 1601461819)

  try {
    const snapshot = await query.get();
    if (!snapshot.exists) {
      // Active cases exist
      const _apptReminderCases = snapshot.docs.map((doc) => doc.data());
}catch (error) {
    console.log("errorInCatchBlock: ", error);
  }

And I get the below error, when I use < or > operators (as you can see in my above code)

The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/...

When I try the below code without the < operator I get the object

const casesRef = db.collection("rch" + "/hospital/cases");
  const query = casesRef
    .where("patientDetails.caseStatus", "==", keys.active)
    .where("patientDetails.apptTimeInEpochSeconds", "==", 1601461818)

Please advice how I could fix this issue.

kurrodu
  • 2,108
  • 4
  • 32
  • 49

1 Answers1

1

Firebase needs indexes to give you these query features. It automatically creates many indexes for you.

And if you are using some kind of compound query which is not created by firebase, you get the following error The query requires an index.

You can just click on the link given after the error which will redirect you to the console and automatically configure the query for you. You just need to build it and after 5 minutes or so (after building), the error would be gone.

Hope it works out!

s_o_m_m_y_e_e
  • 406
  • 4
  • 9