0
votes

I have a collection called 'equipment' and I've set up a cloud function whenever an update is triggered on any document within that collection.

However, I want to update a field on said document within that onUpdate trigger.

By the looks of it. When I either of the below. It is updating the property of the document which is in turn triggering the cloud function again, causing a loop to happen.

event.data.ref.set({property: updatedValue}, {merge: true})

or

event.data.ref.update({property: updatedValue})

Is there any other way of updating a variable inside an onUpdate trigger without it causing this loop?

Thanks

1
You definitely need to implement some logic that prevents the update loop when it should stop. This is a common concern for Cloud Functions Database or Firestore triggers that write back to the same location that triggers them. - Doug Stevenson

1 Answers

1
votes

You'll want to check if the new value is different from the existing value. If they're different, perform the update. If they're the same, skip the update.