这是我的第一个发布的问题,如果我没有正确格式化,请尝试忍受!
我有一个应用程序我在哪里工作,我连接到多个数据库,其中有多个集合,所以我想将mongoose和我的连接(db)传递给包含我的mongoose模式的许多文件那些不同的收藏品,继承我的文件......
// routes.js
[
'label' => 'Keys',
'value' => function($model, $key, $inde){
if(!empty($model->keys))
return '<strong>User don\'t have any key at the moment.</strong>';
$keys = '';
foreach($model->keys as $key):
$keys .= '<strong>' . $key->key . '</strong>';
endforeach;
return $keys;
}
],
// userNotifications.js(mongoose,connectionTwo)
var mongoose = require('mongoose');
var connectionTwo = require('./connectionTwo');
var UserNotifications = require('./models/userNotifications.js')(mongoose, connectionTwo);
var userEvents = require('./models/userEvents.js')(mongoose, connectionTwo);
// userEvents.js(mongoose,connectionTwo)
var Schema = mongoose.Schema;
var userNotificationsSchema = new Schema({
ID: String,
etc: String
}, { collection: 'userNotifications' });
module.exports = connectionTwo.model('userNotifications', userNotificationsSchema);
我想知道是否可以访问传递给我的模式文件的变量,而不必像这样进行...
var Schema = mongoose.Schema;
var userEventsSchema = new Schema({
ID: String,
etc: String
}, { collection: 'userEvents' });
module.exports = connectionTwo.model('userEvents', userEventsSchema);
我只想从这些文件中导出模型,如此
module.exports = function (mongoose, connectionTwo) {
// userNotifications.js content / userEvents.js content
}
所以回到// routes.js我可以像这样轻松查询mongodb
module.exports = connectionTwo.model('userNotifications', userNotificationsSchema);
任何人都可以告诉我最好的方法来解决这个问题吗?以前工作正常我每个数据库只有一个模式/集合,我需要mongoose和每个模式文件中的数据库连接...但是因为我已经将其他模式/集合添加到我的一个数据库中,所以因为它没有从数据库中获取任何数据,所以不再使用该结构,所以我认为这是因为我在多个文件中连接到同一个数据库,这就是我如何考虑传递与每个模式文件的连接......但我现在对它感到困惑......
提前感谢任何可以挽救这一天的人!
答案 0 :(得分:0)
You don't have to export a function, you can export an object.
So if I understand you correctly you can do something like
module.exports = {
notification: connectionTwo.model'userNotifications', userNotificationsSchema),
events: contenctionTwo.model('userEvents', userEventsSchema)
}