【问题标题】:Function returned undefined, expected Promise or value Firebase log error函数返回未定义、预期的 Promise 或值 Firebase 日志错误
【发布时间】:2018-09-27 09:18:48
【问题描述】:

我正在尝试使用 firebase-function 和 node.js 在我的应用程序上添加推送通知,并且一切正常,就像我从发件人那里收到通知一样。但我唯一担心的是日志给了我这个错误

Function returned undefined, expected Promise or value 

这是我的代码:

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 : ', user_id);

    const afterData = change.after.val();

    if (!afterData){
        return console.log('A notification has been deleted from the database', notification_id);
    }

    const fromUser = admin.database().ref(`/notifications/${user_id}/${notification_id}`).once('value');
    return fromUser.then(fromUserResult => {

        const from_user_id = fromUserResult.val().from;


        console.log('You have a new notification from: ', from_user_id);

        const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');

        return deviceToken.then(result => {

            const token_id = result.val();

            const payload = {
                notification: {
                    title: "New Friend Request",
                    body: "You've received a new Friend Request",
                    icon: "default"
                }
            };

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

        });

    });

});

我应该在这里返回什么以及它会放在哪里?我目前正在使用最新的 firebase CFM 1.0 版

【问题讨论】:

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


    【解决方案1】:

    此行可能导致错误消息:

    return console.log('A notification has been deleted from the database', notification_id);
    

    当这行被命中时,你实际上是从函数中返回了undefined,因为这就是console.log() 返回的内容。相反,您应该在日志之后只使用return null

    【讨论】:

    • 更改为 null 后仍然点击“函数返回未定义、预期的 Promise 或值”
    猜你喜欢
    • 2018-04-18
    • 2018-12-03
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多