假设我在我的 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/