Say i have a document A. I have a cloud functions which listens for updates in document A. Once the document A gets updated the cloud functions triggers and also updates the document A. So document A gets updated by the cloud function. Will this update trigger the cloud function again, hence running in a loop?
1 Answers
I have a cloud functions which listens for updates in (a) document... The cloud function also updates the document (which triggered it).
Will this update trigger the cloud function again, hence running in a loop?
Yes the document update done via the Cloud Function with trigger the Cloud Function again, since you defined it to be triggered for each change (with onUpdate()
or onWrite()
).
To avoid an infinite loop, you should compare the value of change.after.data()
(i.e. the new field values) with the value of change.before.data()
(i.e. the field values before the Cloud Function was triggered) and decide if you execute the Cloud Function's business logic or if you exit the Cloud Function without any action.
Don't forget that the two data objects mentioned above may be returned with their properties in a different order. See Object comparison in JavaScript.

- 79,263
- 10
- 95
- 121