Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Node js error, "myFunction" is not a function

So I am using the fcm-node package in order to send notifications from the Express api route to the app using a registration token.

The function is:

const FCM = require('fcm-node');
const serverKey = ...
const fcm = new FCM(serverKey);

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

    fcm.send(message, (err, response) => {
        if (err) console.log('Error ', err)
        else console.log('response ', response)
    });
};

module.exports = {
    sendNotification
};

I made sure that if outside the function, the notification system is running. Now In the api endpoint:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const sendNotification = require('../sendNotification');

router.get('/test', async (req, res, next) => {
  sendNotification('...', 'hi', 'bye','1', '2');
  return res.send(200)
};

I keep on getting the error "sendNotification" is not a function. What is the cause of this?

>Solution :

Expression require('../sendNotification'); is giving you a object (because you exported a object in this file), so extract what you need out.

const { sendNotification } = require('../sendNotification');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading