javascript - 错误消息负载
问题描述
Firebase
当数据库中的数据更新时,我尝试通过函数发送通知Firebase
。我猜这是我在有效负载中引用变量的方式,但我无法识别错误。下面是logcat。
Error sending message: { Error: Messaging payload contains an invalid
value for the "data.name" property. Values must be strings.
at FirebaseMessagingError.Error (native)
at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)
at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:85:28)
at new FirebaseMessagingError (/user_code/node_modules/firebase-admin/lib/utils/error.js:241:16)
at /user_code/node_modules/firebase-admin/lib/messaging/messaging.js:782:27
at Array.forEach (native)
at /user_code/node_modules/firebase-admin/lib/messaging/messaging.js:779:32
at Array.forEach (native)
at Messaging.validateMessagingPayload (/user_code/node_modules/firebase-admin/lib/messaging/messaging.js:772:21)
at /user_code/node_modules/firebase-admin/lib/messaging/messaging.js:609:37
errorInfo:
{ code: 'messaging/invalid-payload',
message: 'Messaging payload contains an invalid value for the "data.name" property. Values must be strings.' },
codePrefix: 'messaging' }
下面是我的 JS 代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.database.ref('/Lecture_Materials/{MIS}/{MISId}/name')
.onWrite((change, context) => {
// Grab the current value of what was written to the Realtime Database.
var eventSnapshot = change.after.val();
var str1 = "Lecture note uploaded is: ";
var str = str1.concat(eventSnapshot.name);
console.log(str);
var topic = "Management.Information.System";
var payload = {
data: {
name: eventSnapshot,
}
};
// Send a message to devices subscribed to the provided topic.
return admin.messaging().sendToTopic(topic, payload)
.then(function(response) {
// See the MessagingTopicResponse reference documentation for the
// contents of response.
console.log("Successfully sent message:", response);
return;
})
.catch(function(error) {
console.log("Error sending message:", error);
});
});
有人帮我吗?我的错误是什么?
解决方案
它应该是这样的
var payload = {
data: {
name: eventSnapshot.name,
}
我对此不确定,但我认为您不能将对象作为有效负载中的值传递。
推荐阅读
- html - 如何将 api 端点添加到 HTML src 标签
- c - STM32L0 定时器 TIM22 以不可预测的方式导致中断?
- spring - 使用 Spring Boot 应用程序访问 postgres docker 容器
- android - 用于将一列与多个值匹配的 SQLite 查询
- java - 如何在 BootstrapDropDown 的项目中添加 onclick 事件
- pyspark - 如何解析时间戳值并更改时间戳值
- java - 如何使用 Vert.x JDBC 客户端管理 Microsoft SQL Server?
- flutter - http.post 被解雇了两次
- android - 如何知道应用程序何时在屏幕 Android 中“现在”可见
- here-api - HERE Maps Api 反向地理编码和街道/道路方位