【问题标题】:read data from childByAutoId in node.js从 node.js 中的 childByAutoId 读取数据
【发布时间】:2020-07-06 23:49:59
【问题描述】:

我正在尝试从由 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;

会做的伎俩,但它没有。

【问题讨论】:

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


    【解决方案1】:

    对于 Cloud Functions 数据库触发器,路径中的通配符与节点名称匹配。他们从不符合任何孩子的价值观。你现在看到的是预期的行为,没有办法改变它。

    如果您想要路径中匹配位置下的子项的值,则必须使用传递给函数的snapshot 参数来访问它。它是一个DataSnapshot 对象,包含路径中匹配的位置下所有子项的所有值。

    在您的情况下,workerId 的值将像这样找到:

    const workerId = snapshot.val().workerId
    

    我建议阅读documentation 以获取有关数据库触发器如何工作的更完整信息。

    【讨论】:

    • 你是最棒的!谢谢道格
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 2015-01-30
    • 2021-08-20
    • 2014-07-03
    相关资源
    最近更新 更多