简体   繁体   English

从 node.js 中的 childByAutoId 读取数据

[英]read data from childByAutoId in node.js

I am trying to read data from firebase that is saved by childByAutoId().我正在尝试从 childByAutoId() 保存的 firebase 读取数据。 I can successfully read the top half, but the reads what the childByAutoId() is.我可以成功读取上半部分,但读取的是 childByAutoId() 是什么。 for example.例如。

图一

and in my firebase functions is在我的 firebase 函数中是

图 2

the logger -- uid is BoSwank... is correct, however on the line below for记录器 -- uid is BoSwank... 是正确的,但是在下面的行中

logger -- workerId is -MBauxL.... is incorrect. logger -- workerId 是 -MBauxL.... 不正确。 That is the value of the childByAutoID() and it should be hkKplzF...这是 childByAutoID() 的值,它应该是 hkKplzF...

How I am trying to read this data is below.我如何尝试读取这些数据如下。

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

I thought changing /notifications/{cardUID}/{workerId} to /notifications/{cardUID}/{cardUID/workerId}我想将 /notifications/{cardUID}/{workerId} 更改为 /notifications/{cardUID}/{cardUID/workerId}

and then changing然后改变

var workerId = context.params.workerId; var workerId = context.params.workerId;

to

var workerId = context.params.cardUID.workerId; var workerId = context.params.cardUID.workerId;

would do the trick but it does not.会做的伎俩,但它没有。

With Cloud Functions database triggers, the wildcards in the path only match the names of nodes.使用 Cloud Functions 数据库触发器,路径中的通配符匹配节点名称。 They never match the values of any children.他们从不符合任何孩子的价值观。 What you are seeing right now is the expected behavior, and there's no way to change it.你现在看到的是预期的行为,没有办法改变它。

If you want the value of children under the location that was matched in the path, you're going to have to reach into it using the snapshot parameter that was passed to the function.如果您想要路径中匹配位置下的子项值,则必须使用传递给 function 的snapshot参数来访问它。 It is a DataSnapshot object, and contains all of the values of all of the children under the location that was matched in the path.它是一个DataSnapshot object,包含路径中匹配的位置下所有子项的所有值。

In your case, the value of workerId is going to be found like this:在您的情况下, workerId的值将如下所示:

const workerId = snapshot.val().workerId

I suggest reading over the documentation for more complete information about how database triggers work.我建议阅读文档以获取有关数据库触发器如何工作的更完整信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM