I've been facing a challenge while attempting to update specific fields within a firebase document. Even though the cloud function triggers and performs an upload on the document, the values of the fields I am trying to update never seem to get uploaded.
The trigger function is set up to execute whenever any field is updated in the "client details" collection. It then retrieves the new field values and creates a reference to access an array that needs to be iterated through.
Subsequently, the document along with its unique identifier are passed to a function that loops through each key-value pair in the updated document and attempts to update the corresponding fields in a different document.
Despite everything running smoothly without encountering any errors, the actual field values remain unchanged.
exports.rebuildFormTriggerClientDetails = functions.firestore.
document('clientDetails/{details}').onUpdate((change) => {
const afterDoc = change.after.data();
const documentId = change.after.id
if (afterDoc)
{
let docUsers = db.collection("clientDetails").doc(documentId);
let caseArray: Array<string>;
caseArray = afterDoc.CaseRefArray;
for (var valCase of caseArray) {
console.log(valCase);
console.log(documentId);
createObjectDocument(docUsers,valCase);
}
}
return Promise
});
function createObjectDocument(document: any, caseNumber: String)
{
document.get().then(function(doc: any) {
if (doc.exists) {
console.log("Document data:", doc.data());
for (let [key, value] of Object.entries(doc.data())) {
console.log(`${key}: ${value}`);
if (key != "CaseRefArray")
{
db.collection("casesToExport").doc(caseNumber).update({key : value });
}
}
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch(function(error: any) {
console.log("Error getting document:", error);
});
[1]: https://i.sstatic.net/duhkq.png