Home > database >  Does cloud function that writing to the same document trigger itself again
Does cloud function that writing to the same document trigger itself again

Time:10-23

For example I have this cloud function

export const testFunction = functions.firestore
  .document('posts/{postID}')
  .onWrite((change) => {
      if (!change.after.data()) return;

      const { count = 0 } = change.after.data();
      t.set(change.after.ref, { count: count   1 }, { merge: true });
  });

Will it be stuck in a loop since it's constantly updating the document field and triggering itself?

CodePudding user response:

Your code does not show what t is, so we can't tell exactly what this function is doing.

But if you are updating a document within a trigger that fired in response to an update on that same document, the trigger will execute again. It's up to you to make the function return without updating the document in order to avoid an infinite loop. So, you will need to figure out how to determine if that's the case.

  • Related