Error with query from Firebase in the .where statement

 ̄綄美尐妖づ 提交于 2020-04-30 07:11:47

问题


I have this function, but I´m having this error with the "where" statement.

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

ERROR: Property 'where' does not exist on type 'AngularFirestoreCollection'.ts(2339)


回答1:


If you want to apply querying in firebase collection. You have to pass queryFn as a second argument to collection method.

Try this:

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



回答2:


if you're using AngularFire2 you can do it like this:

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

If it's plain Firebase SDK

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


来源:https://stackoverflow.com/questions/61181944/error-with-query-from-firebase-in-the-where-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!