我有兩個檔案alarm.js和notifications.js. 在alarm.js我需要呼叫一個名為sendPushfrom的方法notifications.js。
我嘗試過
的:從以下位置匯出函式notifications.js:
module.exports.sendPush = function(params){
console.log("sendPush from notifcations.js called");
console.log(params);
}
將其匯入alarm.js并使用它:
let helperNotif = require('./notifications')
router.post("/", async (req, res) => {
const params = {
param1: 'a',
param2: 'b'
}
helperNotif.sendPush(params)
});
問題:
我不斷收到錯誤訊息helperNotif.sendPush is not a function
問題:
如何從我的檔案中呼叫此notification.js sendPush函式?alarm.js
[編輯] 也許我應該補充一點,notifications.js我有一些router.get,router.post最后module.exports = router;
uj5u.com熱心網友回復:
如果您notifications.js以 結尾module.exports = router,那將覆寫您的module.exports.sendPush = .... 如果要同時匯出 therouter和 the sendPush,可以撰寫
function sendPush(params){
console.log("sendPush from notifcations.js called");
console.log(params);
}
...
module.exports = {router, sendPush};
要將路由器匯入其他地方,您必須撰寫
const {router} = require("./notifications.js");
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/475830.html
標籤:javascript 表示 快速路由器
