使用AngularFire在where子句中无法按预期方式工作

问题描述

目前,我正在开发一个角度项目,需要从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.firestore是AngularFirestore类型。

解决方法

您必须返回更新后的引用/查询对象。在这种情况下,它应该是这样的

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

或者简化,如果你只有一个地方

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