Node js错误,"myFunction"不是函数



所以我使用fcm-node包,以便使用注册令牌从Express api路由发送通知到应用程序。

函数是:

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

我确定如果在功能之外,通知系统正在运行。现在在api端点:

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

我一直得到错误"sendNotification"不是一个函数。这是什么原因呢?

表达式require('../sendNotification');给您一个对象(因为您在此文件中导出了一个对象),因此提取您需要的内容。

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

try this:

module.exports = sendNotification

,并像这样使用:

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

相关内容

最新更新