问题描述
我希望每次从 firebase 数据库修改聊天时,都会激活此功能sendNotification",但会出现此错误:
sendNotificationReferenceError:receiverId 未定义在exports.sendNotification.functions.database.ref.onWrite.event (/user_code/index.js:11:29)在 cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23)在 cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20)在/var/tmp/worker/worker.js:730:24在 process._tickDomainCallback (internal/process/next_tick.js:135:7)
我有这个 javascript 代码,但我不知道为什么它告诉我没有定义 receiverId,非常感谢.
let functions = require('firebase-functions');让 admin = require('firebase-admin');admin.initializeApp();export.sendNotification = functions.database.ref('/Chat/{userId}/{messageId}').onWrite((change, context) => {//获取接收通知的人的userId,因为我们需要获取他们的令牌const receiverId = context.params.userId;console.log("receiverId:",receiverId);//获取发送消息的人的用户IDconst senderId = context.data.child('user_id').val();console.log("senderId:", senderId);//获取消息const message = context.data.child('message').val();console.log("消息:", 消息);//获取消息ID.我们将在有效负载中发送它常量 messageId = context.params.messageId;console.log("messageId:", messageId);//查询用户节点,获取发送消息的用户名return admin.database().ref("/users/" + senderId).once('value').then(snap => {const senderName = snap.child("name").val();console.log("发件人姓名:", 发件人姓名);//获取接收消息的用户的tokenreturn admin.database().ref("/users/" + receiverId).once('value').then(snap => {const token = snap.child("messaging_token").val();console.log("token:", token);//我们有我们需要的一切//构建消息payload并发送消息console.log("构造通知消息.");常量有效载荷 = {数据: {data_type: "direct_message",标题:来自"+发件人姓名的新消息,消息:消息,message_id:messageId,}};return admin.messaging().sendToDevice(token, payload).then(函数(响应){return console.log("成功发送消息:", response);}).catch(函数(错误){return console.log("发送消息出错:", error);});});});});
显然您使用的是旧版本的 Firebase SDK for Cloud Functions,即 <到 1.0 版,但您的语法 (onWrite((change, context))
) 对应于 >= 1.0 版.
图像显示 onWrite.event 上的错误对应于旧语法 (.onWrite((event))
).
您应该更新项目中的 SDK,如下所示:
npm install firebase-functions@latest --savenpm install firebase-admin@latest --save-exact
您还应该将 Firebase CLI 更新到最新版本:
npm install -g firebase-tools
有关所有详细信息,请参阅此文档item.
I want that every time that chat is modified from the firebase database this function is activated "sendNotification" but this error appears:
sendNotification
ReferenceError: receiverId is not defined
at exports.sendNotification.functions.database.ref.onWrite.event (/user_code/index.js:11:29)
at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20)
at /var/tmp/worker/worker.js:730:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
I have this javascript code and I do not know why it tells me that receiverId is not defined, thank you very much.
let functions = require('firebase-functions');
let admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.database.ref('/Chat/{userId}/{messageId}').onWrite((change, context) => {
//get the userId of the person receiving the notification because we need to get their token
const receiverId = context.params.userId;
console.log("receiverId: ", receiverId);
//get the user id of the person who sent the message
const senderId = context.data.child('user_id').val();
console.log("senderId: ", senderId);
//get the message
const message = context.data.child('message').val();
console.log("message: ", message);
//get the message id. We'll be sending this in the payload
const messageId = context.params.messageId;
console.log("messageId: ", messageId);
//query the users node and get the name of the user who sent the message
return admin.database().ref("/users/" + senderId).once('value').then(snap => {
const senderName = snap.child("name").val();
console.log("senderName: ", senderName);
//get the token of the user receiving the message
return admin.database().ref("/users/" + receiverId).once('value').then(snap => {
const token = snap.child("messaging_token").val();
console.log("token: ", token);
//we have everything we need
//Build the message payload and send the message
console.log("Construction the notification message.");
const payload = {
data: {
data_type: "direct_message",
title: "New Message from " + senderName,
message: message,
message_id: messageId,
}
};
return admin.messaging().sendToDevice(token, payload)
.then(function(response) {
return console.log("Successfully sent message:", response);
})
.catch(function(error) {
return console.log("Error sending message:", error);
});
});
});
});
Apparently you are using an old version of the Firebase SDK for Cloud Functions, which is < to version 1.0, but your syntax (onWrite((change, context))
) corresponds to version >= 1.0.
The image shows an error on onWrite.event which corresponds to the old syntax (.onWrite((event))
) .
You should update the SDK in your project, as follows:
See this documentation item for all the details.
这篇关于Firebase函数如何在Android中发送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!