0
votes

I've been developing a simple web app using MongoDB, Express and NodeJS. As of yesterday, I can't seem to connect to MongoDB and keep getting the following error on the terminal:

(node:28265) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [Mon goNetworkError: getaddrinfo ENOTFOUND localhost localhost:27017]

at Pool. (/Users/Mendis/Desktop/UMISC_Website/node_modules/mongodb-core/lib/topologies/server.js:564:11)

at Pool.emit (events.js:182:13) at Connection. (/Users/Mendis/Desktop/UMISC_Website/node_modules/mongodb-core/lib/connection/pool.js:317:12)

at Object.onceWrapper (events.js:273:13)

at Connection.emit (events.js:182:13)

at Socket. (/Users/Mendis/Desktop/UMISC_Website/node_modules/mongodb-core/lib/connection/connection.js:246:50)

at Object.onceWrapper (events.js:273:13)

at Socket.emit (events.js:182:13)

at emitErrorNT (internal/streams/destroy.js:82:8)

at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)

at process._tickCallback (internal/process/next_tick.js:63:19)

(node:28265) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an asy nc function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

(node:28265) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are no t handled will terminate the Node.js process with a non-zero exit code.

Here is a snippet from my app.js file up until the mongoDB connection:

/*=========================package/schema imports=============================*/

var passportLocalMongoose = require("passport-local-mongoose"),
    methodOverride        = require("method-override"),
    localStrategy         = require("passport-local"),
    bodyParser            = require("body-parser"),
    nodemailer            = require("nodemailer"),
    passport              = require("passport"),
    mongoose              = require("mongoose"),
    express               = require("express"),
    seedDB                = require("./seeds"),
    app                   = express();

var Event = require("./models/event"),
    User  = require("./models/user");

/*==================================app config================================*/

// connect to umisc database
mongoose.connect("mongodb://localhost:27017/umisc", {useNewUrlParser: true});

Since I'm fairly new to backend development, I'm really unsure of what I've done wrong...

2
it might be that your mongo instance is not runningKieper
@Kieper I already verified that it's running by using mongo on the terminal. The error shows up only when I run node app.jsnugget_boi

2 Answers

2
votes

The error you are getting is because you have an exception that you do not catch and bubbles up and crashes your instance for security reasons. The issue you are having is most likely due to not being able to connect to the mongo instance.

Try catching the error for a start:

mongoose.connect('mongodb://xxxxxxx.xxxx', function(err, db) {
    if (err) {
        console.log('Unable to connect to the server. Please start the server. Error:', err);
    } else {
        console.log('Connected to Server successfully!');
    }
});

Update: If the local connection fails then try using 127.0.0.1 instead of localhost. Sometimes issues may arise when the local hostname has been changed.

0
votes
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const express = require('express');
const app =express();
var bodyParser = require('body-parser');
app.use(bodyParser.json());
mongoose.connect('mongodb://localhost:27017/newTodos', {useNewUrlParser: true});