发送通知时,firebase函数中的TypeError

时间:2018-08-05 10:29:51

标签: javascript android firebase firebase-realtime-database google-cloud-functions

代码

'use strict'

const functions = require('firebase-functions');

const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.database.ref(`/Notifications/{user_id}/{notification_id}/`).onWrite(event =>{
const user_id = event.params.user_id;
const notification_id = event.params.notification_id;

console.log('We have a notification to send to ', user_id);

if(!event.data.val()){
return console.log("A Notification has been deleted from the database", notification_id);
}


const deviceToken = admin.database().ref(`/UserData/${user_id}/TokenID`).once('value');
return deviceToken.then(result =>{

const token_id = result.val();

const payload ={
notification: {
title: "Friend request",
body: "You have recieved a new Friend Request",
icon: "default"
}
};

return admin.messaging().sendToDevice(token_id, payload).then(response =>{
return console.log('This was the notofication Feature');
});

});

});

错误

enter image description here

一个应用程序的所有代码都可以在Java中使用Java在android中完成,但功能应该在javascript中...这是该语言的新功能,因此不确定错误的含义是什么...有人可以帮助我解决它吗?好吗?

1 个答案:

答案 0 :(得分:1)

更改此:

const functions = require('firebase-functions');

const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.database.ref(`/Notifications/{user_id}/{notification_id}/`).onWrite(event =>{
const user_id = event.params.user_id;
const notification_id = event.params.notification_id;

console.log('We have a notification to send to ', user_id);

if(!event.data.val()){
  return console.log("A Notification has been deleted from the database", notification_id);
}

对此:

 const functions = require('firebase-functions');

const admin = require('firebase-admin');

admin.initializeApp();

exports.sendNotification = functions.database.ref(`/Notifications/{user_id}/{notification_id}/`).onWrite((change,context) =>{
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;

console.log('We have a notification to send to ', user_id);

if(!change.after.val()){
return console.log("A Notification has been deleted from the database", notification_id);
}

更多信息在这里:

https://firebase.google.com/docs/functions/beta-v1-diff