Firestore Error: The query requires an index
Asked Answered
F

6

8

I am facing some problem when use angular 2 to query data from Firestore.

Could anyone please help me by checking it and tell me where I have done wrong ?

My error:

ERROR Error: The query requires an index. You can create it here: https://console.firebase.google.com/project/admin-e8a7b/database/firestore/indexes?create_index=EgR0ZW1wGgcKA3VpZBACGg0KCXN0YXJ0ZWRBdBADGgwKCF9fbmFtZV9fEAM at new FirestoreError (vendor.bundle.js:19925)

This is my code:

getTemp(uid): Observable<any> {
let temps = [];
var today = new Date();
return this.afs.collection<Temps>('temp', ref => ref.where('uid', '==', uid).orderBy('startedAt', 'desc')).snapshotChanges()
  .map(actions => {
    return actions.map(action => {
      const data = action.payload.doc.data() as Temps;
      console.log(data.temp);
      return data.temp;
    });
  });
}
Friedman answered 6/11, 2017 at 17:19 Comment(1)
I need your help !Friedman
P
22

This error is happening because by default Firestore does not require additional indexes for queries that only use equality clauses, therefore that is not your case.

To make your query work you only need to click on the link of the error message and create a new index for your query in the Firebase Console.

To get to know more about how Firestore indexes work you can read on the documentation.

Piggy answered 25/12, 2017 at 22:42 Comment(0)
M
4

Click on the link provided. If it doesn't take you to the correct page, if you get this error:

The project xxx either does not exist, or user doesn't have permission

sign out the current user, and login as your Firebase user.

You will get the index suggested for your query.

Composite indexes are required for queries that include specific values and a range or order.

When you run these queries, they would have bad performance and wouldn't scale well.

Every time you change your query with different fields, combining range, equal to or order by operators, you will have to create a new index. If you only use whereEqualTo operators you don't need to.

It might take a while to create the index depending on your data size.

Michiko answered 15/8, 2018 at 17:25 Comment(0)
P
0

This error may also be happening because one of the fields you queried does not exist in a document scanned

Pellicle answered 23/4, 2018 at 20:34 Comment(0)
T
0

From doc:

Managing indexes

Cloud Firestore ensures query performance by requiring an index for every query. The indexes required for the most basic queries are automatically created for you. As you use and test your app, Cloud Firestore generates error messages that help you create additional indexes your app requires. This page describes how to manage your single-field and composite indexes.

Create a missing index through an error message

If you attempt a compound query with a range clause that doesn't map to an existing index, you receive an error. The error message includes a direct link to create the missing index in the Firebase console.

Read more there.

IF you are in VS code or any IDE, just press ctrl + left click it will bring you to create missing index. If that did not bring you to the create index dialog. Copy paste the url to the browser.

Torquemada answered 15/12, 2019 at 3:7 Comment(0)
W
0

If you are using Firebase hosting you can add the following to your firestore.indexes.json file:

{
  "collectionGroup": "temp",
  "queryScope": "COLLECTION",
  "fields": [
    {
      "fieldPath": "uid",
      "order": "ASCENDING"
    },
    {
      "fieldPath": "startedAt",
      "order": "DESCENDING"
    }
  ]
}

When you deploy Firebase it will automatically build these indexes.

You can alternatively deploy the indexes by themselves with this command:

firebase deploy --only firestore:indexes

Wolverhampton answered 15/12, 2019 at 12:8 Comment(1)
Here, how to add Exemptions for a Single-field indexing?Communalize
T
0

After clicking the given link to update those indexes you might want to back those up to your local repository for later deployment to a different environment using:

firebase firestore:indexes --database=DB_NAME > firestore.indexes.json

You can omit the --database flag if you just want to use the (default) database.

This, as well as the rules file is captured during deployment via firebase.json file:

{
  "$schema": "https://raw.githubusercontent.com/firebase/firebase-tools/master/schema/firebase-config.json",
  "firestore": [
    {
      "database": "DB_NAME",
      "rules": "firestore.rules",
      "indexes": "firestore.indexes.json"
    }
  ],

The schema line can be added to support linting by VSCode or otherwise for convenience.

Once again database field allows you to target a specific database. If you want to stick to (default) you can get rid of the array and directly define the rules and indexes:

{
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },

On deployment you can simply deploy everything as defined in firebase.json or specific portions using the --only directive:

firebase deploy --only firestore:indexes,firestore:rules

In the above case you want to deploy just the firestore indexes and firestore rules.

Note: You must include the firestore: prefix each time, otherwise run both deployments as separate commands.

Tosspot answered 28/6 at 8:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.