NodeJS + MongoDB Error: read ECONNRESET Issue


I know my network is little unstable, but i want to make my mongo driver retry forever how can i make that change?
Currently i am getting following exception and even my network is back mongo driver is not establishing the connection.

Error: read ECONNRESET
    at exports._errnoException (util.js:1024:11)
    at TCP.onread (net.js:610:25)
(node:90835) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: res.Stats is not a function
(node:90835) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

1 Answer

7 years ago by

You can use 'reconnectTries' & 'autoReconnect' parameters to make your connection retry forever.
Update your mongo connection code to the following

var db = {};
var mongoDbUri = "mongodb://db_user:db_password@host:27017/db_name";
var settings = {
      reconnectTries : Number.MAX_VALUE,
      autoReconnect : true
};
MongoClient.connect(mongoDbUri, settings, function(err, dbref) {
  if (!err) {
    console.log("Mongodb connected");
    db = dbref;
  }else{
    console.log("Error while connecting to mongoDB" + err);
  }
});
7 years ago by Karthik Divi