Exporting Multiple Objects In Node.js
I'm working on a MEAN stack project, and trying to pass a set of variables from one controller to another. My current approach is to export multiple objects for my **artworks.js**
Solution 1:
The problem is that required modules in NodeJS are cached. From Node's documentation:
Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.
So you don't need to require
the file again if you want the updated value, and you can move it outside the request callback.
You should instead export a function that returns the array, like this:
module.exports = {
Router: router,
artists: function () {
return distinctArtistsArray
}
}
and then
console.log(ArtworkSearch.artists())
You could also manually update the exported value in your code that periodically updates the distinctArtistsArray
by running:
module.exports.artists = distinctArtistsArray
This needs to be done every time you update the distinctArtistsArray
.
Post a Comment for "Exporting Multiple Objects In Node.js"