0
votes

I'm new to express/node and web programming in general. What is the best way to deal with this error when mongoose's mongodb connection times out, this is how I connect:

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');
});

This is the error when it times out while my server is running:

error connecting with mongodb database: Error: connection timeout at Db. (C:\Users\Sean\OneDrive\webpages\000\lasttry\node_modules\mongoose\lib\drivers\node-mongodb-native\connection.js:169:17) at emitTwo (events.js:106:13) at Db.emit (events.js:191:7) at Server.listener (C:\Users\Sean\OneDrive\webpages\000\lasttry\node_modules\mongodb\lib\db.js:1798:14) at emitOne (events.js:96:13) at Server.emit (events.js:188:7) at Server. (C:\Users\Sean\OneDrive\webpages\000\lasttry\node_modules\mongodb\lib\server.js:274:14) at emitOne (events.js:96:13) at Server.emit (events.js:188:7) at Pool. (C:\Users\Sean\OneDrive\webpages\000\lasttry\node_modules\mongodb-core\lib\topologies\server.js:335:12) at emitOne (events.js:96:13) at Pool.emit (events.js:188:7) at Connection. (C:\Users\Sean\OneDrive\webpages\000\lasttry\node_modules\mongodb-core\lib\connection\pool.js:270:12) at Connection.g (events.js:291:16) at emitTwo (events.js:106:13) at Connection.emit (events.js:191:7)

2
When does the timeout occur? I've only ever had mongoose timeout if the node process gets interupted via my computer going to sleep. - matt
I'm using mLab, its a cloud mongodb service. It seems to timeout randomly. I've only ever seen it happen twice but if I go to production and it happens that would be a nightmare lol. - seanEd
(See the docs.)[docs.mlab.com/timeouts/#connection-timeout] You need to set a connection timeout value like in the answer I posted. Otherwise if there is any small issue with the connection, It won't wait before timing out. - matt
Thanks for the answer +1 - seanEd
No problem. Goodluck with your development - matt

2 Answers

5
votes

How about on disconnect just reconnect to mongo. See below:

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;
});

You can also set a timeout value on the connection.

mongoose.connect(url, { server: { socketOptions: { connectTimeoutMS: 1000 }}}, function(err) { ... });

Also, make sure that mongo is still running on your machine. A connection timeout could mean mongo isn't running.

Reference: Another stack overflow question

0
votes
  1. Check mongod is running.

    typing mongo in shell.

  2. Add connectTimeoutMS=300000 paramter for you uri.

    uri looks like mongodb://localhost/collectionName?connectTimeoutMS=300000