如何在模块之间更新此变量的值
How do I update the values of this variable between modules?
所以我有一个模块"bot.js",在这个模块中,它不断检查消息并将它们分配给一个变量(db_users)。由于我从"app.js"运行我的应用程序,并且我传递了不断填充db_users的函数,我如何将这些信息传递给"app.js"?
Bot.js正在使用IRC函数来存储用户的消息。
var db_users = []
// I then populate db_users with the previous data that is already in mongodb
// using a .find().exec() mongodb command.
bot.addListener('message', function (from, to, text) {
userInfo.checkForUser(db_users);
// checkForUser basically looks through the variable db_users to see if
// there is a username that matches the "from" parameter in the listener
// If it's not there, push some user information into the db_users array
// and create a new MongoDB record.
}
所以我有所有这些,但我的主要应用程序是一个可以控制这个"机器人"的网站(它不是垃圾邮件机器人,而是审核/统计机器人),我正在使用一个require函数在"app.js"中使用"./bot.js"
app.js
bot = require('./bot');
那么,我该如何不断地使用bot.js和app.js中的数据呢?我对模块的工作方式有点模糊。
是的,我可以把app.js的所有内容都放在bot.js中,但浏览起来太烦人了。
谢谢!
将db_users
放入对象中,使其成为一个引用。改为更改该引用。然后export
那个外部对象。现在,由于db_users
只是一个参考,它将始终是它所指内容的最新副本
bot.js
var data = module.exports = {};
data.db_users = [];
bot.addListener('message', function (from, to, text) {
userInfo.checkForUser(data.db_users);
}
app.js
botData = require('./bot');
botData.db_users
将始终具有bot.js
中对data.db_users
进行的任何最新更改
相关文章:
- React:使用索引变量更新嵌套集合
- 范围变量更新,但不更新视图
- 正在将代码库中的C#变量更新到asp.net
- 使用变量更新剑道图表系列的数据会导致重绘将列从图表上绘制出来
- MongoDB 用变量更新数组
- 将变量更新为函数中的参数
- Javascript-变量更新
- 使用Javascript和PHP变量更新页面上的数据
- Angular js 在
标签上的变量更新$scope动画
- 如何通过参数变量更新对象的属性值
- 如何在范围变量更新时更新模板
- 简单的变量更新和写入页
- 函数在变量更新后的某个时间点执行
- javascript OO如何用一些JSON变量更新self参数
- 使用$set中的变量更新mongodb的node.js
- 将声明的null变量更新为布尔值
- 在查询部分使用变量更新查询
- 如何在回调中使用变量更新文档?
- 通过AJAX用PHP变量更新URL
- 防止在没有javascript的情况下在页面刷新后进行变量更新