首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >消息有效负载时出错

消息有效负载时出错
EN

Stack Overflow用户
提问于 2018-06-06 14:37:19
回答 1查看 255关注 0票数 -1

Firebase数据库中的数据更新时,我尝试通过Firebase函数发送通知。我猜这是因为我在有效负载中引用了变量,但我无法识别错误。下面是logcat。

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
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);
            });
    });

有没有人帮我一下?我的错误是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-06 15:23:03

应该是这样的

代码语言:javascript
复制
 var payload = {
          data: {
            name: eventSnapshot.name,
          }

我不确定这一点,但我认为你不能将对象作为一个值传递到有效负载中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50713706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档