错误:连接超时-expressjs mongodb mongoose
一般来说,我对express/node和web编程是新手。当mongoose的mongodb连接超时时,处理此错误的最佳方法是什么,以下是我的连接方式:错误:连接超时-expressjs mongodb mongoose,mongodb,express,mongoose,Mongodb,Express,Mongoose,一般来说,我对express/node和web编程是新手。当mongoose的mongodb连接超时时,处理此错误的最佳方法是什么,以下是我的连接方式: mongoose.connect(config.mongoUrl); const db = mongoose.connection; db.on('error', console.error.bind(console, 'error connecting with mongodb database:')); db.once('open', f
mongoose.connect(config.mongoUrl);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'error connecting with mongodb database:'));
db.once('open', function() {
console.log('connected to mongodb database');
});
这是服务器运行时超时时出现的错误:
连接mongodb数据库时出错:错误:连接超时
在分贝。(C:\Users\Sean\OneDrive\webpages\000\lastry\node\u modules\mongoose\lib\drivers\node mongodb native\connection.js:169:17)
两点钟(events.js:106:13)
在Db.emit(events.js:191:7)
在Server.listener(C:\Users\Sean\OneDrive\webpages\000\lastry\node\u modules\mongodb\lib\db.js:1798:14)
在emitOne(events.js:96:13)
在Server.emit上(events.js:188:7)
在服务器上。(C:\Users\Sean\OneDrive\webpages\000\lasttry\node\u modules\mongodb\lib\server.js:274:14)
在emitOne(events.js:96:13)
在Server.emit上(events.js:188:7)
在游泳池。(C:\Users\Sean\OneDrive\webpages\000\lasttry\node\u modules\mongodb core\lib\topologies\server.js:335:12)
在emitOne(events.js:96:13)
在Pool.emit(events.js:188:7)
在连接处。(C:\Users\Sean\OneDrive\webpages\000\lasttry\node\u modules\mongodb core\lib\connection\pool.js:270:12)
在Connection.g(events.js:291:16)
两点钟(events.js:106:13)
在Connection.emit(events.js:191:7)如何断开连接,只需重新连接到mongo。见下文:
mongoose.connect(config.mongoUrl);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'error connecting with mongodb database:'));
db.once('open', function() {
console.log('connected to mongodb database');
});
db.on('disconnected', function () {
//Reconnect on timeout
mongoose.connect(config.mongoUrl);
db = mongoose.connection;
});
您还可以设置连接的超时值
mongoose.connect(url, { server: { socketOptions: { connectTimeoutMS: 1000 }}}, function(err) { ... });
另外,请确保mongo仍在您的计算机上运行。连接超时可能意味着mongo没有运行
参考资料:在断开连接后如何,只需重新连接到mongo即可。见下文:
mongoose.connect(config.mongoUrl);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'error connecting with mongodb database:'));
db.once('open', function() {
console.log('connected to mongodb database');
});
db.on('disconnected', function () {
//Reconnect on timeout
mongoose.connect(config.mongoUrl);
db = mongoose.connection;
});
您还可以设置连接的超时值
mongoose.connect(url, { server: { socketOptions: { connectTimeoutMS: 1000 }}}, function(err) { ... });
另外,请确保mongo仍在您的计算机上运行。连接超时可能意味着mongo没有运行
参考:
mongo
connectTimeoutMS=300000
参数
uri看起来像mongodb://localhost/collectionName?connectTimeoutMS=300000
mongo
connectTimeoutMS=300000
参数
uri看起来像mongodb://localhost/collectionName?connectTimeoutMS=300000
超时何时发生?我只在节点进程通过我的计算机进入睡眠状态而被中断时才会出现mongoose超时。我使用的是mLab,它是一个云mongodb服务。它似乎是随机超时的。我只见过两次这样的事情发生,但如果我去生产,这将是一个噩梦lol。(见文档。)[您需要设置一个连接超时值,如我发布的答案中所示。否则,如果连接出现任何小问题,它将不会等待超时。感谢您的回答+1没有问题。祝您在开发过程中好运,什么时候会发生超时?如果节点进程通过我的计算机g被中断,我只会遇到mongoose超时我正在睡觉。我正在使用mLab,这是一个云mongodb服务。它似乎是随机超时的。我只见过它发生两次,但如果我去生产,它发生了,那将是一个噩梦lol。(见文档。)[您需要设置一个连接超时值,如我发布的答案所示。否则,如果连接出现任何小问题,它将不会等待超时。感谢您的回答+1没问题。祝您发展顺利