通过更改从键访问值(onWrite)

时间:2019-03-22 22:25:33

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

我正在尝试从Firebase实时数据库访问键(节点)的值。我正在使用onWrite触发器,该触发器传递更改参数,该参数保存触发器之前和之后的数据库快照。因此,尽管使用了After触发器快照,但我希望将节点的密钥放入一个常量变量中,尽管我尝试接收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);

我希望notifyValid变量保留'true'的值。

Firebase数据库

enter image description here

1 个答案:

答案 0 :(得分:0)

不需要代码中的.ref

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

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

相关问题