1

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • See https://stackoverflow.com/questions/43784744/cloud-functions-for-firebase-get-parent-data-from-database-trigger, https://stackoverflow.com/questions/43784744/cloud-functions-for-firebase-get-parent-data-from-database-trigger/45384501#45384501 and https://stackoverflow.com/questions/45491967/firebase-get-parrent-of-parent-data-before-delete – Frank van Puffelen Aug 30 '17 at 23:29

2 Answers2

2

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();
    });
});
Jen Person
  • 7,356
  • 22
  • 30
  • You sure this would work? The path is only listing to .../likes, how would it know the value for the key name? – J. Doe Aug 30 '17 at 22:24
  • Unfortunately, I get `TypeError: event.data.ref.parent.child(...).val is not a function` –  Aug 30 '17 at 22:24
  • @J.Doe dont confuse. i dont want key name! –  Aug 30 '17 at 22:26
  • Jen. Unfortunately, I get `TypeError: event.data.ref.parent.child(...).val is not a function` Thanks for your answer thogh –  Aug 30 '17 at 22:28
  • @Kolop copy and paste Jen's answer, it works. You need to add a **value listener** if you want to get the name data. You can't just call `event.data.ref.parent.child('name').val`. – Bradley Mackey Aug 31 '17 at 08:31
  • @BradleyMackey okay. but what should I do if I want to get different `const`'s at once? –  Aug 31 '17 at 10:10
  • @BradleyMackey okay. but what should I do if I want to get different `const`'s at once? –  Sep 01 '17 at 17:11
0

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();
});

});

kemalony
  • 195
  • 1
  • 7