【问题标题】:Accessing Value from Key via Change (onWrite)通过更改从键访问值(onWrite)
【发布时间】:2019-08-13 23:14:09
【问题描述】:

我正在尝试从 firebase 实时数据库中访问键(节点)的值。我正在使用传递更改参数的 onWrite 触发器,它在触发器之前和触发器之后保存数据库的快照。因此,对于后触发器的快照,我希望将节点的键放入常量变量中,尽管当我尝试收到 TypeError 时。

类型错误错误:

TypeError: change.after.ref.child(...).val 不是函数 在exports.sendNotification.functions.database.ref.onWrite (/user_code/index.js:13:64)`

这意味着 .val() 不能以我尝试使用它的方式使用,尽管我看不到任何其他信息来了解如何处理它。

相关代码段:

'user-strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase); // load the firebase-functions and firebase-admin modules, and initialize an admin app instance from which Realtime Database changes can be made.

exports.sendNotification = functions.database.ref("/Notifications/{user_id}/{notification_type}/{sender_id}").onWrite((change, context) => {

    const user_id = context.params.user_id;
    const notification_type = context.params.notification_type;
    const sender_id = context.params.sender_id;

    //Check if the notification is still to be sent (true not false)
    const notficationValid = change.after.ref.child(sender_id).val();
    console.log(`Notification Valid: `, notficationValid);

我希望 notificationValid 变量保持“true”的值。

Firebase 数据库

【问题讨论】:

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


    【解决方案1】:

    不需要您代码中的.ref

    change.after.child(sender_id).val()
    

    DataSnapshot 上调用.ref 会返回一个DatabaseReference,它没有val() 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-29
      相关资源
      最近更新 更多