GuyC
GuyC

Reputation: 1019

Firebase Cloud Functions onDelete - How to access parent's information?

So, from Firebase functions, I'm listening to this event -

exports.populateVairations_delete =
functions.database.ref('/parentA/parentB/child').onDelete(event => 
{
    // I know how to get the previous value for what I'm listening too...
    val = event.data.previous.val();
    ...
}

This function is being invoked also when deleting the parent, which is exactly what I want.

But when deleting a parent, how do I access data from /parentA before it's being deleted?

Upvotes: 1

Views: 1055

Answers (3)

irwing844
irwing844

Reputation: 11

With context.resource.name you could get a string containing the data path.

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317457

onDelete triggers are always executed after the delete has occurred. There's no way to prevent a delete from happening with a function. Your onDelete code will be delivered an event that contains only the data that was deleted. The event object itself can't be used to see other parts of the database.

If you need to access other parts of the database inside a database trigger, you can use the Admin SDK to make those queries. There is a lot of official sample code that illustrates how to do this.

Upvotes: 1

Farmaan Elahi
Farmaan Elahi

Reputation: 1650

Just use Admin SDK for Firebase, You can have administrator access to the firebase Db.From there, you can do basically anything with the Firebase Db

Upvotes: 0

Related Questions