【问题标题】:Obtain raw value "val()" data of parent node from a child reference in Cloud Functions Realtime Database从 Cloud Functions 实时数据库中的子引用获取父节点的原始值“val()”数据
【发布时间】:2019-04-13 23:46:55
【问题描述】:

假设我在 typescript/javascript 代码函数中指向如下路径:

exports.sendNotification = functions.database.ref('shops/countries/{countryName}/shopAddress/{currentShopAddress}')
.onWrite((snapshot,context) => {

    // Is it possible to get the data raw value from a child reference node? 
    // For example: 
    const countryName = snapshot.before.parent('countryName').val();
    // Then
    const countryId = countryName['countryId'];
})

我是 node.js/typescript 和 firebase 云功能新手 :)

【问题讨论】:

    标签: node.js typescript firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    数据库中父节点的数据不会自动传递到您的云函数中,因为您可能是大量不需要的数据。

    如果需要,您需要自己加载。幸运的是,这并不难:

    const countryRef = snapshot.ref.parent.parent;
    const countryName = countryRef.key; // also available as context.params.countryName
    countryRef.child('countryId').once('value').then((countryIdSnapshot) => {
      const countryId = countryIdSnapshot.val();
    });
    

    请注意,由于您异步加载额外数据,您需要返回一个承诺以确保您的函数不会过早关闭。

    【讨论】:

    • snap.parent.parent 或 snap.parent 不存在
    • 好的。修正了那个错字。
    猜你喜欢
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    • 2020-05-12
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    相关资源
    最近更新 更多