我写了一个网页,很简单。两张静态图片和一段存储在Mongodb中的文字。但是在不断刷新网页时会出现下面的错误: Error: db object already connecting, open cannot be called multiple times at Db.open (X:\BYSJ\Project\simulat\node_modules\mongodb\lib\mongodb\db.js:224:11) at exports.index (X:\BYSJ\Project\simulat\routes\index.js:16:12) at callbacks (X:\BYSJ\Project\simulat\node_modules\express\lib\router\index.js:161:37) at param (X:\BYSJ\Project\simulat\node_modules\express\lib\router\index.js:135:11) at pass (X:\BYSJ\Project\simulat\node_modules\express\lib\router\index.js:142:5) at Router._dispatch (X:\BYSJ\Project\simulat\node_modules\express\lib\router\index.js:170:5) at Object.router (X:\BYSJ\Project\simulat\node_modules\express\lib\router\index.js:33:10) at next (X:\BYSJ\Project\simulat\node_modules\express\node_modules\connect\lib\proto.js:190:15) at Object.methodOverride [as handle] (X:\BYSJ\Project\simulat\node_modules\express\node_modules\connect\lib\middleware\methodOverride.js:37:5) at next (X:\BYSJ\Project\simulat\node_modules\express\node_modules\connect\lib\proto.js:190:15)
我的代码如下: exports.index = function(req, res){ // var dbtest=require(‘dbcon’); var mongodb=require(‘mongodb’) , mongoserver=new mongodb.Server('localhost’,27017) ,dbtest=new mongodb.Db('test’,mongoserver,{w:-1});
// console.log(dbtest.serverConfig);
var pas;
dbtest.open(function(err,db){
if(err){
// console.log(db)
console.log("OPen Db Failed");
//db.close();
return res.redirect("/error");
} else{
db.collection('test',function(err,coll){
if(err){
console.log("Slect Collection Failed");
db.close();
return res.redirect("/error");
}
coll.find({title:"test"}).toArray(function(err,doc){
if(err){
console.log("query data Failed");
db.close();
return res.redirect("/error") ;
}
pas=doc[0].con;
//console.log(doc);
res.locals.pas=pas;
db.close();
res.render('index', { title: 'Express' });
})
});
}
});
};
请问问题出在哪啊?
@dingwei 我发现了一个问题,但是不知道怎么解释。就是如果将poolSize配置为大于1的数字,那么在进行那个并发测试时,就会有一半的open操作失败。但是如果把poolSize配置为1,就不会出现can’t connect to database的错误。而且poolSize即使设置为100也会有一半的链接出错。