Search code examples
angularfirebaseangularfire

where clause not working as expected in angular using AngularFire


Currently I am working on an angular project where I need to filter records from firebase. I querying firebase with where clause. But It is returning all documents from collection hence not working.

Here is my query

 this.firestore.collection('cities', ref => { 
  // ref.where("country", '==', "India");
  ref.where("state", "==", "Tripura");

  return ref;
})
.get().toPromise().then((querySnapshot) => { 
  querySnapshot.forEach((doc) => {
       console.log(doc.id, "=>", doc.data());  
  }); 
});

where this.firestoreis AngularFirestore of type.


Solution

  • You have to return the updated ref/query object. In this case it should look like this

    this.firestore.collection('cities', ref => { 
      let query = ref.where("country", '==', "India");
      query = ref.where("state", "==", "Tripura");
      return query;
    })
    

    Or simplified if you have only one where

    this.firestore.collection('cities', ref => 
      ref.where("state", "==", "Tripura")
    )