0

So I have the following notification function,

async function sendNotification(registrationToken, title, body) {
    const message = {
        to: registrationToken,
        notification: {
            title: title,
            body: body
        }
    };

    await fcm.send(message, (err, response) => {
        if (err) {
            return err;
        }
        else {
            return response;
        }
    });
};

When I call the function like this in the api

let result = await sendNotification('','','');

result is always undefined. How do I solve this issue?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Nour Mawla
  • 301
  • 1
  • 3
  • 14
  • 1
    You can't do this as is. As we said [in the previous question](https://stackoverflow.com/questions/72123331/node-js-firebase-notification-return-the-json-response-in-a-function-format) `fcm.send` is not a promise API, but a callback one. First "promisify" `fcm.send` and then return it's result from `sendNotification` – evolutionxbox May 05 '22 at 08:38
  • I wasn't able to solve the issue by refereeing to the previous question. If you know what piece of code I must write in order to be able to output the json response of firebase feel free to add it please. Otherwise please allow others to help me – Nour Mawla May 05 '22 at 08:40
  • 1
    You really need to read the accepted answer to https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call. – evolutionxbox May 05 '22 at 08:45
  • How is `fcm` in your code initialized? – Frank van Puffelen May 05 '22 at 14:29

0 Answers0