Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore index error

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;
    });
  });
}
like image 521
Tri Nguyen Avatar asked Nov 06 '17 17:11

Tri Nguyen


Video Answer


2 Answers

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.

like image 85
Mateus Forgiarini da Silva Avatar answered Oct 23 '22 14:10

Mateus Forgiarini da Silva


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.

like image 39
live-love Avatar answered Oct 23 '22 14:10

live-love