Made a cloud function which will send email of invitation and upon successful send it will update a map object field but for some reason it is only working for the last object not all of them
Function Code
exports.sendInvitationEmailForMembers = functions.firestore
.document("/organization-members/{OMid}")
.onWrite(async (snap, context) => {
if (snap.after.data() === undefined) {
return null;
}
let authData = nodeMailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
user: SENDER_EMAIL,
pass: SENDER_PASSWORD,
},
});
// Exit when the data is deleted.
const id = context.params.OMid;
const afterData = snap.after.data();
const db = admin.firestore();
for (var prop in afterData) {
if (afterData[prop].status === "invited") {
const email = afterData[prop].email;
const name = afterData[prop].fullName;
authData
.sendMail({
from: "[email protected]",
to: `${email}`,
subject: "Organization Team Invitation",
text: `Dear ${name}, you have been invited to join the team of 'organization name', please click on the following link to sign up and join. <br> http://localhost:3000/`,
html: `Dear ${name}, you have been invited to join the team of 'organization name', please click on the following link to sign up and join. <br> http://localhost:3000/`,
})
.then(() => {
db.collection("organization-members")
.doc(id)
.update({ [[prop] + ".status"]: "pending" })
.then(() => {
console.log("Successfully updated");
})
.catch(() => {
console.log("Error occured while updating invitation status");
});
})
.catch((err) => console.log(err.message));
}
}
});
Notice that only last object status was changed but it should've changed the whole document objects statuses