node.js - 从 Cloud Functions 实时数据库中的子引用获取父节点的原始值“val()”数据
问题描述
假设我在 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 云功能新手 :)
解决方案
数据库中父节点的数据不会自动传递到您的 Cloud Function 中,因为您可能是大量不需要的数据。
如果你需要它,你需要自己加载它。幸运的是,这并不难:
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();
});
请注意,由于您异步加载其他数据,因此您需要返回一个承诺以确保您的函数不会过早关闭。
推荐阅读
- javascript - d3.event 在组合模块时不起作用
- c# - 在 ac# mvc 应用程序中启动计划作业
- pandas - pandas中迭代函数的值
- angularjs - 离子错误:不幸的是应用程序已停止
- wpf - SelectedIndex 不通过双击更新 WPF TabControl,但在按下按钮时更新
- css - CSS Counter 在 Chrome 中使用 slidetoggle 给出错误的编号
- c# - UWP 应用程序 - 如何将响应列表保存到文件中并取回数据
- c# - Func<> 动态添加一个额外的参数
- html - 使用 AngularJS 弹出直方图窗口
- javascript - 使用 socket.io 接收事件并更新元素 html