Using the OP's .then()
style, gather the get/update promises and execute them together with Promise.all()
.
exports.writeToFirestore = functions.firestore
.document('/magazzino_esterni_2022/{esternoId}')
.onCreate((snap, context) => {
let items = snap.data().magazzino;
let promises = items.map(item => {
console.log("start update", item);
const itemName = item.data().name; // OP must check this.. use whatever data prop contains 'GIACCA_3XS'
return updateMagazineCount(itemName);
});
// here's the punch line: run the promises concurrently before finishing
return Promise.all(promises).then(() => {
console.log("end update")
});
});
// for clarity, a separate function that gets and updates
function updateMagazineCount(itemName) {
const itemDataRef = firestore.collection("magazzino_articoli_2022").doc(itemName);
return itemDataRef.get().then(doc => {
const count = doc.data().count + 1;
console.log("update to ", count);
return itemDataRef.update({ count });
}).then(result => {
console.log("update completed");
return result;
});
}
If you prefer the newer async / await
style, you should still gather promises in a collection and use Promise.all(). (Don't await
them in a loop unless they must be performed sequentially).
exports.writeToFirestore = functions.firestore
.document('/magazzino_esterni_2022/{esternoId}')
.onCreate(async (snap, context) => {
let items = snap.data().magazzino;
let promises = items.map(item => {
console.log("start update", item);
const itemName = item.data().name; // OP must check this.. use whatever data prop contains 'GIACCA_3XS'
return updateMagazineCount(itemName);
});
await Promise.all(promises)
console.log("end update")
});
async function updateMagazineCount(itemName) {
const itemDataRef = firestore.collection("magazzino_articoli_2022").doc(itemName);
const doc = await itemDataRef.get()
const count = doc.data().count + 1;
console.log("update to ", count);
const result = await itemDataRef.update({ count });
console.log("update completed");
return result;
}
To perform the get/updates sequentially, build a chain of promises with then, or in the newer style, you would await in a loop.
// then style
exports.writeToFirestore = functions.firestore
.document('/magazzino_esterni_2022/{esternoId}')
.onCreate(async (snap, context) => {
let items = snap.data().magazzino;
let promise = Promise.resolve();
for (let item of items) {
console.log("start update", item);
const itemName = item.data().name; // OP must check this.. use whatever data prop contains 'GIACCA_3XS'
promise = promise.then(() => updateMagazineCount(itemName));
}
return promise.then(() => {
console.log("end update")
});
});
Or using async style...
exports.writeToFirestore = functions.firestore
.document('/magazzino_esterni_2022/{esternoId}')
.onCreate(async (snap, context) => {
let items = snap.data().magazzino;
for (let item of items) {
console.log("start update", item);
const itemName = item.data().name; // OP must check this.. use whatever data prop contains 'GIACCA_3XS'
await updateMagazineCount(itemName);
}
console.log("end update", item)
});