[英]read data from childByAutoId in node.js
我正在尝试从 childByAutoId() 保存的 firebase 读取数据。 我可以成功读取上半部分,但读取的是 childByAutoId() 是什么。 例如。
在我的 firebase 函数中是
记录器 -- uid is BoSwank... 是正确的,但是在下面的行中
logger -- workerId 是 -MBauxL.... 不正确。 这是 childByAutoID() 的值,它应该是 hkKplzF...
我如何尝试读取这些数据如下。
exports.observeNotifications = functions.database.ref('/notifications/{cardUID}/{workerId}').onCreate((snapshot, context) => {
var uid = context.params.cardUID;
var workerId = context.params.workerId;
console.log('LOGGER --- uid is ' + uid);
console.log('LOGGER --- workerId is ' + workerId)
})
我想将 /notifications/{cardUID}/{workerId} 更改为 /notifications/{cardUID}/{cardUID/workerId}
然后改变
var workerId = context.params.workerId;
至
var workerId = context.params.cardUID.workerId;
会做的伎俩,但它没有。
使用 Cloud Functions 数据库触发器,路径中的通配符仅匹配节点名称。 他们从不符合任何孩子的价值观。 你现在看到的是预期的行为,没有办法改变它。
如果您想要路径中匹配位置下的子项值,则必须使用传递给 function 的snapshot
参数来访问它。 它是一个DataSnapshot object,包含路径中匹配的位置下所有子项的所有值。
在您的情况下, workerId
的值将如下所示:
const workerId = snapshot.val().workerId
我建议阅读文档以获取有关数据库触发器如何工作的更完整信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.