一尘不染

MongoError:与本地主机的连接0:27017超时

node.js

events.js:141
      throw er; // Unhandled 'error' event
MongoError: connection 0 to localhost:27017 timed out
at Function.MongoError.create (/home/ubuntu/scripts/node_modules/mongodb-core/lib/error.js:29:11)
    at Socket.<anonymous> (/home/ubuntu/scripts/node_modules/mongodb-core/lib/connection/connection.js:184:20)
    at Socket.g (events.js:260:16)
    at emitNone (events.js:67:13)
    at Socket.emit (events.js:166:7)
    at Socket._onTimeout (net.js:318:8)
    at _runOnTimeout (timers.js:524:11)
    at _makeTimerTimeout (timers.js:515:3)
    at Timer.unrefTimeout (timers.js:584:5)

嗯,连接期间没有错误,但是当尝试保存一些模型/集合时,它会运行一段时间,然后抛出此错误。顺便说一句,我还有另一个连接到同一mongodb服务器的节点进程。非常感谢您的帮助。


阅读 315

收藏
2020-07-07

共1个答案

一尘不染

const mongoose = require('mongoose');
const option = {
    socketTimeoutMS: 30000,
    keepAlive: true,
    reconnectTries: 30000
};

const mongoURI = process.env.MONGODB_URI;
mongoose.connect(mongoURI, option).then(function(){
    //connected successfully
}, function(err) {
    //err handle
});
2020-07-07