【问题标题】:where clause not working as expected in angular using AngularFire使用 AngularFire 的 where 子句在 Angular 中没有按预期工作
【发布时间】:2020-09-27 06:53:55
【问题描述】:

目前我正在做一个 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.firestore 是 AngularFirestore 类型。

【问题讨论】:

    标签: angular firebase angularfire


    【解决方案1】:

    您必须返回更新后的 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")
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      相关资源
      最近更新 更多