user8540345
user8540345

Reputation:

Get data from parent in Cloud Function?

I want to assign to a const the name value inside parent

exports.myFunction = functions.database.ref('/messages/{pushId}/likes')

.onWrite(event => {

    const name = event.parent.data.val().name; // this doesn't work. how do I properly get the name which is located in /messages/{pushId} ?
    });

screenshot of the situation

Upvotes: 1

Views: 2155

Answers (2)

kemalony
kemalony

Reputation: 195

after version update

event <- (change, context)

event.data <-change.after

exports.myFunction = functions.database.ref('/messages/{pushId}/likes')
.onWrite((change, context) => {
 return change.after.ref.parent.child('name').once('value').then(snapshot => {
    const name = snapshot.val();
});

});

Upvotes: 0

Jen Person
Jen Person

Reputation: 7546

According to the documentation, this is how you access data from another path:

exports.myFunction = functions.database.ref('/messages/{pushId}/likes')
.onWrite(event => {
     return event.data.ref.parent.child('name').once('value').then(snapshot => {
        const name = snapshot.val();
    });
});

Upvotes: 2

Related Questions