Skip to content Skip to sidebar Skip to footer

Value Of Firebase Functions Onwrite Sometimes Is Null

I'm trying to implement an Android ordering system and I'm using Realtime Database. To send notifications I'm using Firebase Functions in JavaScript. Each user is stored in the

Solution 1:

onWrite will trigger for any change that was matched by the location of the trigger. This means it will trigger for any create, update, or delete.

When the triggering event was a creation of new data, change.before will be undefined (the was no data before), and change.after will contain a snapshot.

When the triggering event is a deletion of data, change.before will contain a snapshot, and change.after will be undefined.

Your code is blindly assuming that change.after contains a snapshot. It's entirely possible that deleting data is triggering your function, and it's failing because that's not what it was designed to handle.

When using onWrite, it's your obligation to check this case and handle it appropriately. If you don't care about delete events, consider using onCreate instead to just capture new data.

Post a Comment for "Value Of Firebase Functions Onwrite Sometimes Is Null"