Firestore get document by id on angular 2

前端 未结 2 762
谎友^
谎友^ 2021-01-01 16:08

How to get document by id in google firestore? is there some get() method? because I searched but didn\'t find a proper answer that suits to me :(

Updat

相关标签:
2条回答
  • 2021-01-01 16:27

    Try this code

    this.itemscollection.doc(id).ref.get().then(function(doc) {
      if (doc.exists) {
        console.log("Document data:", doc.data());
      } else {
        console.log("No such document!");
      }
    }).catch(function(error) {
      console.log("Error getting document:", error);
    });
    
    0 讨论(0)
  • 2021-01-01 16:35

    the key to success working with angular 2( when using the package angularFire2 ) with firestore is to know that all the firestore methods in their official documention that manipulate single doc like 'get' 'set' 'update' are child of the 'ref' method Example insted

     firestore - this.itemscollection.doc(id).get() 
     angularfirestore2 - this.itemscollection.doc(id).ref.get()
    -------------------------------------
     firestore - this.itemscollection.doc(id).set()
     angularfirestore2 - this.itemscollection.doc(id).ref.set()
    
    0 讨论(0)
提交回复
热议问题