Function returned undefined, expected Promise or value Firebase log error

牧云@^-^@ 提交于 2020-01-14 13:06:10

问题


Im trying to add a push notification on my app using firebase-function and node.js and all its all working fine, like I got notification from the sender. but my only concern is that the log gave me this error

Function returned undefined, expected Promise or value 

and this is my code:

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');
            });

        });

    });

});

what should I return here and where it will be place? I am currently using the latest firebase CFM version 1.0


回答1:


This line could be causing the error message:

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

When this line is hit, you're effectively returning undefined from the function, because that's what console.log() returns. Instead, you should just return null after the log.



来源:https://stackoverflow.com/questions/49877553/function-returned-undefined-expected-promise-or-value-firebase-log-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!