部署node.js云功能时出错

时间:2018-11-07 22:30:36

标签: node.js firebase firebase-cloud-messaging google-cloud-functions firebase-notifications

我是我的应用中firebase的实施通知。 这是我的node.js函数

'use strict'

const functions = require('firebase-functions');
const admin=require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotificaiton=functions.database.ref('/Notifications/{userKey}/{notification_id}').
onWrite(event => {
const userKey = event.params.userKey;
const notification = event.params.notification;

console.log('The userKey is ', userKey);

});

我的firebase数据库结构是 enter image description here

函数错误为

enter image description here

请帮助我。 预先感谢

1 个答案:

答案 0 :(得分:2)

更改代码

从此

exports.sendNotificaiton=functions.database.ref('/Notifications/{userKey}/{notification_id}').
onWrite(event => {
const userKey = event.params.userKey;
const notification = event.params.notification;

console.log('The userKey is ', userKey);

});

对此

exports.sendNotificaiton=functions.database.ref('/Notifications/{userKey}/{notification_id}').onWrite((change, context) => {

const userKey = context.params.userKey;
const notification = context.params.notification;

console.log('The userKey is ', userKey);

});

您正在为event使用旧的onWrite()触发器,现在您需要传递上下文和dataSnapshot(更改)。

当写入事件触发您的数据库时,onWrite的值分别为beforeafter

在此处检查文档:https://firebase.google.com/docs/functions/database-events?hl=en

有关通知,请参见github上的通知示例:https://github.com/firebase/functions-samples/tree/Node-8/fcm-notifications