Riazbapoo
Riazbapoo

Reputation: 61

Firebase cloud functions - get parent node while listening for changes on children nodes

I am listening for changes on a particular child node (wallet) for all children in my firebase database.

when any of the wallet nodes across all the values in my database are modified I want to know about it and I want to execute a specific function

screenshot of database

I require the parent node of the child (wallet) which was just modified

I have tried the following code

exports.executeFunction = functions.database.ref('/database/{pushId}/wallet').onWrite((change, context) => {
    //execute my function
}

so for example, in the attached screenshot. when the value of wallet is modified in this instance I want to be able to get "rFA2VPdFpSSzdj4JzhmNFogW10x2" as a result as it is the parent node.

How do I go about doing this?

Upvotes: 0

Views: 690

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317372

change.before is a DataSnapshot object which has a ref property. This is a Reference object that you can use to navigate up the nodes to the parent you want. change.before.ref.parent will be a Reference pointing to the parent node you're looking for.

Upvotes: 4

Related Questions