Search code examples
node.jsmongodbexpressmongoosemlab

mongoose to mlab connection issues : UnhandledPromiseRejectionWarning: Unhandled promise rejection


enter image description here

const express = require('express');
const app = express();
const mongoose = require('mongoose');
require('./models/users');
require('./services/passport');
const authRoutes = require('./routes/authRoutes');
const Keys = require('./config/dev');

authRoutes(app);

mongoose.connect(Keys.MONGOOSE_URI);

const port = process.env.PORT || 5000;
app.listen(port);

I have a node express application and I trying to connect to mlab db using mongoose. I am getting following exception:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoNetworkError: failed to connect to server [ds111598.mlab.com:11598] otworkError: connect ECONNREFUSED 35.168.9.109:11598]

Tech stack I am using:

"mongoose": "^4.11.1",
node: 6.11.1
npm: 4.6.1

I have tried using the latest mongoose version as well but still the same issue. I am stuck because of this. Can anybody help in this?

Thanks


Solution

  • If you are under corporate proxy, you must have to come out else you can check my code.

     const options = {
        useMongoClient: true,
        autoIndex: false, // Don't build indexes
        reconnectTries: 100, // Never stop trying to reconnect
        reconnectInterval: 500, // Reconnect every 500ms
        poolSize: 10, // Maintain up to 10 socket connections
        // If not connected, return errors immediately rather than waiting for reconnect
        bufferMaxEntries: 0
      };
    mongoose.connect('mongodb://user:xxxxxx@dsxxxx.mlab.com:667799/DB_NAME',options).then(
      ()=>{
        console.log("connected to mongoDB")},
     (err)=>{
         console.log("err",err);
    }
    

    )