I am working on a Firebase cloud function to update a field value for every element of the array I provide.
Below is my loop.
I would like for it to add a document to another collection as soon as this loop has been completed.
// FOR EACH ITEM
sortedArray.forEach(([key, val]) => {
walletRef.doc('j10wmCUhUWPxYJpIElBxmFAEI6l1').update({
[val.product]: admin.firestore.FieldValue.increment(val.quantity),
})
})
When I try to add a .then like below, it fails
// FOR EACH ITEM
sortedArray.forEach(([key, val]) => {
walletRef.doc('document_id_here').update({
[val.product]: admin.firestore.FieldValue.increment(val.quantity),
})
}).then((doc) => {
logsRef.add({
user_id: "document_id_here",
product: items,
transactionType: "purchase",
date: new Date(),
})
});
Logged error:
textPayload: "Function execution took 2594 ms, finished with status: 'crash'"
The field values are updated but the new document is not created. Please help. Thanks.