1

I know that I can create a new query to read the doc by id in callback. But can I get the whole snapshot in the callback after creating document or at least TIMESTAMP?

firebase.firestore().collection("comments").add({
  body: data
})
.then(comment => {
  console.log(comment);
})
.catch(error => {
  console.log(error);
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
rendom
  • 3,417
  • 6
  • 38
  • 49

1 Answers1

5

Calling CollectionRef.add(...) return a reference to the newly created document. To be able to access the data of that new document you'll need to still load it. So:

firebase.firestore().collection("48486654").add({
  timestamp: firebase.firestore.FieldValue.serverTimestamp()
})
.then(function(docRef) {
  docRef.get().then(function(doc) {
    console.log(doc.data().timestamp.toString());
  });
})
.catch(function(error) {
  console.error(error);
});

For a working example, see: https://jsbin.com/xorucej/edit?js,console

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Yeah, I know that I can retrieve the doc after creating in separate query. What I'm asking is more util information in response callback, for example servertime of document creation. So we will not have to make another call just to get it. – rendom Jan 29 '18 at 05:35