Skip to content

Instantly share code, notes, and snippets.

@fiveisprime
Created June 8, 2015 13:33
Show Gist options
  • Save fiveisprime/311cbbdd690aabbd9685 to your computer and use it in GitHub Desktop.
Save fiveisprime/311cbbdd690aabbd9685 to your computer and use it in GitHub Desktop.
Reconnect on error with mongoose.
db.on('error', function(error) {
console.error('Error in MongoDb connection: %j', error);
mongoose.disconnect();
});
db.on('disconnected', function() {
console.log('MongoDB disconnected!');
mongoose.connect(config.mongo.uri, config.mongo.options);
});
//
// Ensure that `options` has `server: { auto_reconnect: true }`
//
mongoose.connect(config.mongo.uri, config.mongo.options);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment