Mariana Reis Silveira
Mariana Reis Silveira

Reputation: 311

Can I avoid triggering a Google Cloud Function?

In my app, every time an user deletes a comment that he/she made in a post, a cloud function is triggered to update some property inside user's record in the users collection. Like: on comment delete, update user's record.

However, when deleting user's account and consequently its comments, that cloud function is triggered, since the comments are being deleted.

Because the account is being deleted, I don't need that update inside the user's collection to happen.

Is there a way of avoiding triggering a Google Cloud Function if a condition is met, like, "if deleting account, return null"?

Can I pass an argument to the function so I can use it as a condition?

Any help will be very appreciated.

Upvotes: 0

Views: 508

Answers (2)

Tarik Huber
Tarik Huber

Reputation: 7418

You can't awoid triggering them but you can detect if the comment is deleted by the user or another cloud function that is "cleaning" user data from a user that gets deleted by checking the auth type like here:

.onUpdate((eventSnapshot, context) => {
    if (context.authType === 'ADMIN') {
      // This cloud function is triggered by another cloud function so we exit here
      return null
    }

If you delete all user data when a user gets deleted by using a cloud function you can use this method to just exit the other cloud functions that would be triggered.

Upvotes: 2

DazWilkin
DazWilkin

Reputation: 40356

IIUC, you're using Cloud Firestore triggers and I assume (!?) you're using OnWrite events.

OnWrite includes create, update and delete.

One solution is to create OnCreate and OnUpdate triggers instead.

If you don't permit your users to update comments, then you just need OnCreate.

Upvotes: 0

Related Questions