0

The code below is what is wrong and it runs up until it gets to the .catch function with Mongo DB. And I can't figure out what is wrong. If i remove the catch and let it error it shall come up with this error -

(node:72993) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]

//Dependicies
var express = require("express");
var mongoose = require("mongoose");
var bodyParser = require("body-parser");
var routes = require('./routes/route');
var app = express();


//MongoDB
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/filmo', { useMongoClient: true }, function () {

console.log("Connected to Database")

}).catch(function () {
  console.log("Not Connected to Database ERROR!");


});


//Routes Listed
app.use('/', routes);


//Start Server
const defaultPortNumber = 8080
const port = process.env.PORT || defaultPortNumber

app.listen(port, err => console.log(err || `API is Running on port ${port}.`))

Added code to see what the error is actually saying via this link - How do I find out where an unhandled promise rejection occured?

And Error now says - MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]

J.David
  • 1
  • 4

1 Answers1

0

You are using some strange mix between ordinary callback and promise handling. When you are unable to connect, which is your problem, you get:

C:\test> node app
API is Running on port 8080.
Connected to Database
Not Connected to Database ERROR!

Because both the callback and the catch function are called.

If you want to handle the promise returned from connect you should use then() and catch() like this:

mongoose.connect('mongodb://localhost/filmo', { useMongoClient: true }).then(() => {
    console.log("Connected to Database");
}).catch((err) => {
    console.log("Not Connected to Database ERROR! ", err);
});

Now it will choose either the then function or the catch function if the connect promise fails.

The actual error is ECONNREFUSED. It means your application is unable to connect to the database. Make sure mongod is running. You will get exactly that error message if it's not. If that's not the case then make sure the port mongod is running on is 27017, as that is the default port if you don't supply it in the connection url.

Finally, the UnhandledPromiseRejectionWarning message is shown when you either are missing the catch function or you're not handling the error in an ordinary callback.

E.g. for ordinary callback:

This would give you UnhandledPromiseRejectionWarning:

mongoose.connect('mongodb://localhost/filmo', { useMongoClient: true }, () => {
    console.log("Connected to Database");
})

This would not:

mongoose.connect('mongodb://localhost/filmo', { useMongoClient: true }, (err) => {
    if (err) throw err;
    console.log("Connected to Database");
})
Mika Sundland
  • 18,120
  • 16
  • 38
  • 50