有时我们的团队通过以下代码连接localhost上的mongodb数据库:
const mongoose = require('mongoose');
const CONN = 'mongodb://name:password@www.wecard.io:27017/myproject'
const option = {
socketTimeoutMS: 30000,
keepAlive: true,
reconnectTries: 30000
};
mongoose.connect(CONN, option).then(
() => { console.log('It is good: ' + CONN); },
(err) => {
console.error('Error: ' + err);
console.log(err);
}
);
我们的一个队友遇到了这个错误:
MongoNetworkError: failed to connect to server [www.website.com:27017] on first connect [MongoNetworkError: connect ETIMEDOUT 46.101.98.56:27017]
at Pool.<anonymous> (/Users/liubaobao/Desktop/WeCard/models/node_modules/mongodb-core/lib/topologies/server.js:505:11)
at emitOne (events.js:116:13)
at Pool.emit (events.js:211:7)
at Connection.<anonymous> (/Users/liubaobao/Desktop/WeCard/models/node_modules/mongodb-core/lib/connection/pool.js:329:12)
at Object.onceWrapper (events.js:317:30)
at emitTwo (events.js:126:13)
at Connection.emit (events.js:214:7)
at Socket.<anonymous> (/Users/liubaobao/Desktop/WeCard/models/node_modules/mongodb-core/lib/connection/connection.js:245:50)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'MongoNetworkError',
message: 'failed to connect to server [www.website.com:27017] on first connect [MongoNetworkError: connect ETIMEDOUT 46.101.98.56:27017]'
与此同时,如果我们将它连接到云服务器上,到目前为止它一直都很好。所以任何人都知道如何解决它?感谢。