It seems like you are looking to ensure that the update process is completed before proceeding with the code execution. When you call update
on a DocumentReference
, it returns a promise that resolves to a WriteResult
. You can simply await for this promise to resolve before moving forward with the rest of the code:
// Define the DocumentReference
const docRef = db.collection("collection").doc("docid");
// Update the document and wait for completion
docRef.update({ message: "hello" }).then(writeResult => {
// Wait for update to complete before displaying WriteResult
console.log(writeResult);
// To confirm that the update is done, fetch the same document from Firestore
return docRef.get();
}).then(documentSnapshot => {
console.log(documentSnapshot.id, "=>", documentSnapshot.data());
// => "docid => { message: 'hello'}"
})
SAME APPROACH USING ASYNC/AWAIT SYNTAX
// Define the DocumentReference
const docRef = db.collection("collection").doc("docid");
// Update the document, wait for completion, and display write result
const writeResult = await docRef.update({ message: "hello" });
console.log(writeResult);
// Confirm update completion by fetching the updated document from Firestore
const documentSnapshot = await docRef.get();
console.log(documentSnapshot.id, "=>", documentSnapshot.data());
// => "docid => { message: 'hello'}"