Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Functions for Firebase: How to get the value of the data that was deleted

When I add a cloud function responding to a delete event, like this one:

exports.onDeleteSector = functions.database.ref('/sectores/{idSector}').onDelete((event) =>

I can get the key to the sector being deleted in event.params.idSector proving the trigger works, however, event.data.val() returns null.

The deleted record contains the references to the children to be deleted. How can I get those before the parent is gone?

Thanks

like image 442
Devasatyam Avatar asked Jul 09 '17 16:07

Devasatyam


Video Answer


1 Answers

event.data.val() returns null, because that's the current value of the database at the time of the trigger. For all kinds of database triggers, this is going to be the case. For onDelete, this will always be null.

If you want to see what was previously at that location, before the event happened, take a look at event.data.previous.val(). Also see the docs for DeltaSnapshot, which is the data type for event.data.

like image 118
Doug Stevenson Avatar answered Sep 25 '22 15:09

Doug Stevenson