从Cloud Function中获取父级数据?

时间:2017-08-30 21:42:27

标签: javascript firebase firebase-realtime-database google-cloud-functions

我想在父

中分配const名称值
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

2 个答案:

答案 0 :(得分:3)

根据the documentation,这是您从其他路径访问数据的方式:

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

答案 1 :(得分:0)

版本更新后

事件< - (更改,上下文)

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

});