Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore get document by id on angular 2

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 :(

Update: this.itemscollection.doc(id).get() didn't worked for me

like image 985
Kek Testerov Avatar asked Nov 10 '17 14:11

Kek Testerov


2 Answers

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);
});
like image 199
Hareesh Avatar answered Oct 19 '22 19:10

Hareesh


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()
like image 30
yehonatan yehezkel Avatar answered Oct 19 '22 17:10

yehonatan yehezkel