【发布时间】:2016-04-02 03:13:57
【问题描述】:
我尝试通过将它变成一个函数并通过返回来将其取出,但没有这样做。
我看到一个例子,有人使用module.exports.VariableName = objects;
我的问题是我仍然无法访问或使用该变量名。例如var names = Collection; 在同一个文件上。
ReferenceError: 集合未定义
我做错了什么?谢谢。
mongoose.connection.on('open', function(ref) {
console.log('Connected to mongo server.');
//trying to get collection names
mongoose.connection.db.listCollections().toArray(function(err, names) {
if (err) {
console.log(err);
} else {
module.exports.Collection = names;
}
});
});
【问题讨论】:
-
如果这个文件的
require在listCollections运行之前运行,那么它就没有Collection键
标签: javascript node.js express mongoose