在 Node.JS 中导出多个对象 [英] exporting multiple objects in Node.JS

查看:98
本文介绍了在 Node.JS 中导出多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个 MEAN 堆栈项目,并试图将一组变量从一个控制器传递到另一个控制器.我目前的方法是为我的 **artworks.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** controller file:

module.exports = {
  Router: router,
  artists: distinctArtistsArray
}

路由器对象是传统的 Express 路由器,但 distinctArtistsArray 是我的 MongoDB 数据库中所有艺术家的字符串数组.我有一个在后台运行的异步函数,它不时地填充 distinctArtistsArray.然后将此数组传递给其他地方编写的其他一些函数,以便在幕后执行一些 NLP 任务.

The router object is a traditional Express router, but the distinctArtistsArray is a String array of all artists in my MongoDB database. I have an asynchronous function running in the background that every now and then populates distinctArtistsArray. This array is then passed into some other functions written elsewhere in order to do some NLP tasks behind the scenes.

我希望另一个控制器也可以访问这些变量,因为我不希望通过对艺术家信息进行重复的异步查询而给我的 MongoDB 带来过度负载.这是我的另一个控制器 (complete.js):

I'd like another controller to have access to these variables as well, since I don't want to be placing undue load on my MongoDB by making duplicate async queries for artist information. Here's my other controller (complete.js):

router.get("/", (req, res) => {
    var ArtworkSearch = require('../controllers/artworks.js');
    console.log(ArtworkSearch.artists)
});

我已经将 require('../controllers/artworks/js') 放在回调函数中,因为我想每次都不断地重新导入值请求命中 /complete 端点.

但是,在我的控制台中,我收到一个空数组 ([]),这是我将 distinctArtistsArray 初始化为的对象.仅供参考,我遵循了与此 SO 帖子中接受的答案相同的方法a> 类似的话题.

However, in my console, I receive an empty array ([]), which is what I initialize distinctArtistsArray to. FYI, I've followed the same exact method as the accepted answer in this SO post on a similar topic.

如何让我的艺术家列表在 complete.js 控制器中可用?

How do I get my list of artists to become available inside complete.js controller?

推荐答案

问题是 NodeJS 中需要的模块被缓存了.来自 Node 的文档:

The problem is that required modules in NodeJS are cached. From Node's documentation:

模块在第一次加载后被缓存.这意味着(除其他外)每次调用 require('foo') 都会返回完全相同的对象,如果它解析为同一个文件.

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.

所以如果你想要更新的值,你不需要再次require文件,你可以将它移到请求回调之外.

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
  }
}

然后

console.log(ArtworkSearch.artists())

您还可以手动更新代码中的导出值,通过运行以下命令定期更新 distinctArtistsArray:

You could also manually update the exported value in your code that periodically updates the distinctArtistsArray by running:

module.exports.artists = distinctArtistsArray

每次更新 distinctArtistsArray 时都需要这样做.

This needs to be done every time you update the distinctArtistsArray.

这篇关于在 Node.JS 中导出多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆