0

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)

5
  • When does the timeout occur? I've only ever had mongoose timeout if the node process gets interupted via my computer going to sleep. Commented Feb 11, 2017 at 15:42
  • 1
    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. Commented Feb 11, 2017 at 17:28
  • (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. Commented Feb 11, 2017 at 17:32
  • 1
    Thanks for the answer +1 Commented Feb 11, 2017 at 18:23
  • No problem. Goodluck with your development Commented Feb 11, 2017 at 18:43

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

0
  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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.