node.js - 从 Cloud Functions Realtime Database 中的子引用获取父 Node 的原始值 "val()"数据

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

假设我在我的 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 的数据不会自动传递到您的云函数中,因为您可能存在大量不需要的数据。

如果你需要它,你需要自己加载它。幸运的是,这并不那么难:

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

请注意,由于您异步加载附加数据,因此您需要返回一个 promise 以确保您的函数不会过早关闭。

关于node.js - 从 Cloud Functions Realtime Database 中的子引用获取父 Node 的原始值 "val()"数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53242119/

相关文章:

javascript - 从回调中获取数据

javascript - Promise.allSettled typescript 数组推理

iOS - Firebase Storage 尝试在 didFinishLaunchingWithOptions 之前初始化

javascript - 使用 Firebase,firebase.auth().currentUser 是 null 值,即使它正在被记录

typescript - 找不到模块 '...' 或其相应的类型声明。 (NextJS + typescript )

android - Firebase firestore 变量名称已更改

javascript - FirebaseError : Function addDoc() called with invalid data. 不支持的字段值:未定义

node.js - promise 在使用列表时所有内容都不会按预期工作

node.js - 如何使用 Sails.js v1 中的 .find() 方法进行不区分大小写的搜索

mysql - 如何在 Node.JS 中使用 SQL