使用 AngularFire 的 angular 中的 where 子句未按预期工作

where clause not working as expected in angular using AngularFire

目前我正在做一个 angular 项目,我需要从 firebase 中过滤记录。我用 where 子句查询 firebase。但它正在返回集合中的所有文档,因此无法正常工作。

这是我的查询

 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());  
  }); 
});

其中 this.firestoreis 类型的 AngularFirestore。

您必须 return 更新后的 ref/query 对象。在这种情况下,它应该是这样的

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

如果你只有一个,则可以简化 where

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