【问题标题】:Get data from parent in Cloud Function?从 Cloud Function 中的父级获取数据?
【发布时间】:2017-08-30 21:42:26
【问题描述】:

我想将父级中的名称值分配给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} ?
    });

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

  • 你确定这行得通吗?该路径仅列出.../likes,它如何知道键名的值?
  • 不幸的是,我收到了TypeError: event.data.ref.parent.child(...).val is not a function
  • @J.Doe 不要混淆。我不想要键名!
  • 仁。不幸的是,我得到了TypeError: event.data.ref.parent.child(...).val is not a function 谢谢你的回答虽然
  • @Kolop 复制并粘贴 Jen 的答案,它有效。如果要获取名称数据,则需要添加 value listener。你不能只打电话给event.data.ref.parent.child('name').val
【解决方案2】:

版本更新后

事件

event.data

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

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 2022-12-06
    • 2020-09-24
    • 1970-01-01
    • 2017-01-04
    • 2020-06-20
    • 1970-01-01
    相关资源
    最近更新 更多