• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

172
Views
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:

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?

about 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

try this:

module.exports = sendNotification

and use it like this:

const sendNotification = require('../sendNotification');
about 3 years ago · Juan Pablo Isaza Report

0

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');
about 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error