Skip to content
Advertisement

How to export and use a function using Express?

I have two file alarm.js and notifications.js. In alarm.js I need to call a method called sendPush from notifications.js.

What I’ve tried :
Exporting the function from notifications.js:

JavaScript

Importing it in alarm.js and use it :

JavaScript

The problem:
I keep getting the error saying helperNotif.sendPush is not a function

The question :
How can I call this notification.js sendPush function from my alarm.js file ?

[EDIT] maybe I should add that in notifications.js I have some router.get and router.post and at the end module.exports = router;

Advertisement

Answer

If your notifications.js ends with module.exports = router, that will overwrite your module.exports.sendPush = .... If you want to export both the router and the sendPush, you can write

JavaScript

To import the router elsewhere, you must then write

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement