Tengo dos archivos alarm.js y notifications.js . En alarm.js necesito llamar a un método llamado sendPush de notifications.js .
Lo que he probado:
Exportando la función de notifications.js :
module.exports.sendPush = function(params){ console.log("sendPush from notifcations.js called"); console.log(params); } Importarlo en alarm.js y usarlo:
let helperNotif = require('./notifications') router.post("/", async (req, res) => { const params = { param1: 'a', param2: 'b' } helperNotif.sendPush(params) }); El problema:
Sigo recibiendo el error que dice helperNotif.sendPush is not a function
La pregunta :
¿Cómo puedo llamar a esta función de notification.js sendPush desde mi archivo alarm.js ?
[EDITAR] tal vez debería agregar que en notifications.js tengo algunos router.get y router.post y al final module.exports = router;
Si tus notifications.js terminan con module.exports = router , eso sobrescribirá tu module.exports.sendPush = ... . Si desea exportar tanto el router como el sendPush , puede escribir
function sendPush(params){ console.log("sendPush from notifcations.js called"); console.log(params); } ... module.exports = {router, sendPush};Para importar el enrutador en otro lugar, debe escribir
const {router} = require("./notifications.js");