我有一个正在运行的Node.js Express Web应用程序。
我有两个数据库连接,system_db是使用"Mongoose.connect"创建的,user_db是使用"Mongoose.createConnection"创建的。这似乎使它们分开并具有不同的连接池。虽然这可能是问题的关键所在。
我有处理Mongoose模型的代码
在模型文件中,我有:
var authSchema = mongoose.Schema({
teamName: String,
league: String,
players: []
});
var Team = module.exports = mongoose.model('teams',authSchema);
在我的主文件中,我有两个连接:
其中一个是系统数据库。
connection_uri = "mongodb://localhost:27017/";
system_db = mongoose.connect(connection_uri.concat("sparks"), {
native_parser : true
}, function(err){
if (err) throw err;
});
另一个是用户数据库
user_db = mongoose.createConnection(connection_uri.concat(user_id));
然后我在主文件中有这段代码,根据id查找Team对象:
app.param('team_id', function(req, res, next, team_id) {
Team.findOne(team_id, function(err, team) {
if (err) {
return next(err);
}
if (!team) {
throw new Error("no team matched");
}
req.team = team;
next();
});
});
问题在于上面的app.param函数没有找到任何与teams匹配的结果,尽管它们存在于user_db数据库的集合中。这意味着,我认为我的模型指向了错误的数据库?不知道是否正确?我该如何解决?