How to handle promise in firestore

2019-07-27 02:36发布

问题:

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.