Skip to content Skip to sidebar Skip to footer

Firebase: Get Document Snapshot After Creating

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.fir

Solution 1:

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


Post a Comment for "Firebase: Get Document Snapshot After Creating"