如何在模块之间更新此变量的值?
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.
}
所以我拥有所有这些,但我的主要应用程序是一个可以控制此 "bot" 的网站(它不是垃圾邮件机器人,而是 moderation/statistical 机器人),我正在使用要求函数在 "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
所做的任何最新更改
所以我有模块"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.
}
所以我拥有所有这些,但我的主要应用程序是一个可以控制此 "bot" 的网站(它不是垃圾邮件机器人,而是 moderation/statistical 机器人),我正在使用要求函数在 "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
所做的任何最新更改