【问题标题】:How to Retrieve individual JSON values from Firebase Database?如何从 Firebase 数据库中检索单个 JSON 值?
【发布时间】:2018-11-20 07:22:13
【问题描述】:

假设我有一个 firebase 节点,即 dbref = firebase.ref('/Transfer_Request/{pushID]/')。

并且客户端写入两个值; from_ID 和 to_ID 到 dbref。如何从 Firebase Cloud 函数中获取 from_ID 和 to_ID 的单独值?

我的代码:

exports.TransferTicket = functions.database.ref('/Transfer_Request/{pushID}').onWrite((event) => {

  const original = event.data.val();
  const from_ID = original.from_ID;
  const to_email_ID = original.to_ID;

  //search for to_email ID
  return admin.database().set("A transfer request was just made");

});

我遇到两个错误:

1)

TypeError: admin.database(...).set 不是函数 exports.TransferTicket.functions.database.ref.onWrite (/user_code/index.js:41:25) 在对象。 (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27) 在下一个(本机)在 /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 在 __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) 在 cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:53:36) 在 /var/tmp/worker/worker.js:716:24 在 process._tickDomainCallback (内部/进程/next_tick.js:135:7)

2)

TypeError:无法读取 null 的属性“来自” exports.TransferTicket.functions.database.ref.onWrite (/user_code/index.js:35:25) 在对象。 (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27) 在下一个(本机)在 /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 在 __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) 在 cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:53:36) 在 /var/tmp/worker/worker.js:716:24 在 process._tickDomainCallback (内部/进程/next_tick.js:135:7)

【问题讨论】:

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


    【解决方案1】:

    第一个问题来自这样一个事实,即在执行以下操作时您会错过 Firebase Reference

    return admin.database().set("A transfer request was just made");
    

    你应该这样做:

    admin.database().ref('...the path where you want to write...').set("A transfer request was just made");
    

    有关更多详细信息,请参阅ReferenceDatabase 的文档。

    第二个问题来自于 Firebase SDK for Cloud Functions 新版本 1.0.0 发布以来,语法发生了变化。请参阅此文档item

    你应该修改你的代码如下:

    exports.TransferTicket = functions.database.ref('/Transfer_Request/{pushID}').onWrite((change, context) => {
    
        const original = change.after.val();
    
        const from_ID = original.from_ID;
        console.log(from_ID);
        const to_email_ID = original.to_ID;
        console.log(to_email_ID);
    
        return admin.database().ref('...path...').set("A transfer request was just made")
        .catch(error => {
            console.log(error);
            //...
        });
    
    });
    

    【讨论】:

    • 非常感谢!我将 .ref('/Transfer_Request/{pushID]/') 处的值输入为 {"from_ID" : "afd@gm.com", "to_ID" : "afds1@gm.com"}。现在,如何将“from_ID”和“to_ID”的值分配给程序中的不同变量?因为它说 'change.after.val()' 为空。这可能是因为它不是一个值,而是一个正在写入的 json 对象吗?
    • 您的 javascript 对象似乎正确。当您从 Firebase 控制台查看数据库时;您看到 from_ID 和 to_ID 的正确值了吗?
    • 您能否在帖子中添加Transfer_Request 节点的数据库摘录?
    • 按您的要求上传!
    • 我看到的唯一奇怪的想法是,在您的打印屏幕上,您错过了 ar@gm.com 和 sd@gm.com 末尾的双引号。其余的它应该工作。我已经用一些控制台日志更新了我的答案。您在函数日志中看到了什么?我还使用 set() 方法的 catch 更新代码,以处理可能的 promise 拒绝。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 2016-11-27
    相关资源
    最近更新 更多