hi,
I stuck in a infinte loop with my trigger. does anyone know how to prevent from getting in the infinite loop?
I have a worktime document in a user realm (partition: user=userId). A admin-member should be allowed to edit the worktime from other user. For that I want to copy or update the worktime into their realms. The trigger runs on the Worktime-Collection so obviously the trigger ends in a loop…
Here my trigger function:
exports = async function(changeEvent) {
console.log(changeEvent.operationType);
const db = context.services.get("project").db("test");
const worktimeCollection = db.collection("Worktime");
const teamCollection = context.services.get("project").db("test").collection("Team");
var worktime = JSON.parse(JSON.stringify(changeEvent.fullDocument));
const docId = changeEvent.documentKey._id;
if (changeEvent.operationType == "insert") {
const team = await teamCollection.findOne({ _id: new BSON.ObjectId(worktime.teamId) });
team.membersAdmin.forEach(userId => {
if (userId !== worktime.createdBy) {
worktime._id = new BSON.ObjectId();
worktime._partition = `user=${userId}`;
worktime._wid = docId;
worktimeCollection.insertOne(worktime).then(() => {
console.log('Document added');
});
}
});
}
if (changeEvent.operationType == "update") {
worktimeCollection.updateMany({ _wid: docId }, worktime, { upsert: true }).then(() => {
console.log('Document updated');
});
}
if (changeEvent.operationType == "delete") {
worktimeCollection.deleteMany({ _wid: docId }).then(() => {
console.log('Document deleted');
});
}
};
thank you