【问题标题】:Error with query from Firebase in the .where statement.where 语句中来自 Firebase 的查询出错
【发布时间】:2020-04-13 05:21:02
【问题描述】:

我有这个功能,但是“where”语句出现这个错误。

 getUser() {
    this.db.collection("users").where("name", "==", "Maria")
}

错误:类型“AngularFirestoreCollection”.ts(2339) 上不存在属性“where”

【问题讨论】:

    标签: angular firebase ionic-framework google-cloud-firestore


    【解决方案1】:

    如果您想在 firebase 集合中应用查询。您必须将 queryFn 作为第二个参数传递给集合方法。

    试试这个:

        this.db.collection("users",(ref) => {
          let query : firebase.firestore.CollectionReference | firebase.firestore.Query = ref;
          query = query.where(`name`, `==`, 'Maria');
          return query;
        } ).valueChanges();
    

    【讨论】:

    • 感谢您的回答,查勒潘。现在,没有错误,但结果是“未定义”。
    • 哦,对不起。非常感谢。问候。
    【解决方案2】:

    如果你使用 AngularFire2,你可以这样做:

    getUser() {
        const query = this.db.collection('users', ref => ref.where('name', '==', 'Maria'); //Remember that the casing matters, Maria !== maria.
        // From here you can do whatever you need, use as Promise or Observable:
        // Promise Based
        query 
           .then(data => {
               return data.map(snaps => {
                   return snaps;
               })
           })
           .catch((err) => console.log(err));
           .finally(() => console.log('completed');
    }
    

    如果是普通的 Firebase SDK

    getUser() {
         this.db.collection('users').where('name', '==', 'Maria'); //Remember that the casing matters, Maria !== maria.
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-09
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      • 2021-05-23
      • 2012-01-23
      • 2016-07-10
      • 1970-01-01
      相关资源
      最近更新 更多