Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
¿Cómo exportar y usar una función usando Express?

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;

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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");
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!