2021年3月26日星期五

is it okay if I intentionally make my cloud functions throw multiple errors?

I have collection and sub-collection like this

users/{userID}/followers/{followerID}  

everytime a follower document is deleted in followers sub-collection, then it will trigger this firestore trigger below to decrease the numberOfFollowers field in user document. this is triggered when a user click unfollow button

exports.onDeleteFollower = functions  .firestore.document("users/{userID}/followers/{followerID}")  .onDelete((snapshot, context) => {         // normally triggered after a user push unfollow button       // then update the user document         const organizerID = context.params.userID;      const updatedData = {          numberOfFollowers: admin.firestore.FieldValue.increment(-1),      };        return db.doc(`users/${userID}`).update(updatedData);    });  

now I have a case like this ....

if a user deletes their account, then I will delete the user document ( users/{userID} ), but if I delete a user document, it will not automatically delete all documents inside its sub-collection, right

so after I delete the user document, I have another function to delete all documents inside the followers sub-collection.

but the problem is, the onDeleteFollower triggers function above will be executed multiple times, and it will throw error multiple times, because the user document has been deleted ( the function above will be used to a update a field in deleted user doc)

I will have this error in functions emulator

⚠  functions: Error: 5 NOT_FOUND: no entity to update: app: "myApp"  path <    Element {      type: "users"      name: "u1-HuWQ5hoCQnOAwh0zRQM0nOe96K03"    }  >    ⚠  Your function was killed because it raised an unhandled error.  

I actually can write a logic to check if a user document still exist or not. if exist then update numberOfFollowers field

but deleting a user document is very rare if compared to a user click the unfollow button, I think it is not very efficient.

I have a plan like this, I will intentionally let the errors happened. say a user has 1000 followers, then it will trigger the onDeleteFollower function above, then I will have 1000 function errors

my question is .....

is it okay if I have multiple errors in a short time like that? will Google Cloud Function terminates my function, or .... I don't know, I am worried something bad will happen that I don't know

I can't let the follower update the organizer (user) document directly from the client app, because it is not safe.

https://stackoverflow.com/questions/66827393/is-it-okay-if-i-intentionally-make-my-cloud-functions-throw-multiple-errors March 27, 2021 at 10:06AM

没有评论:

发表评论