user8724610
user8724610

Reputation:

Function returned undefined, expected Promise or value Firebase log error

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

Upvotes: 4

Views: 881

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317828

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.

Upvotes: 2

Related Questions