How to handle promise in firestore

好久不见. 提交于 2019-12-11 07:25:57

问题


here is my code to check a doc is there in the collections

this.shopCollection = this.afs.collection<Shop>('shops', ref => {
  return ref.where('fulladdress', '==', myString)
});

i don't know why i can't use .then() and .catch() with this method. When i pass a string to query and if no result found how i know?

I am using angularfire2 version ^5.0.0-rc.1 with angular ^4.2.4 and firebase ^4.5.0.

Please help.


回答1:


I got it working with some changes

Here is my code

this.afs.collection<Shop>('shops').ref.where('fulladdress', '==', myString)
      .get()
      .then(res => {
        if(res.docs.length == 0){
          //no documents found
        }else{
          //you got some documents
          res.forEach(shop => {
            console.log(shop.id);
            console.log(shop.data());
          })
        }
      }).catch(err => {
        console.log('something went wrong '+ err)
      });

i am not sure this is the right way, works fine for me.



来源:https://stackoverflow.com/questions/46748513/how-to-handle-promise-in-firestore

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