user10342594
user10342594

Reputation:

(node:8592) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): MongoParseError: Invalid connection string

My code was running fine but this happened when I ran it today:

(node:8592) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): MongoParseError: Invalid connection string

Here is a snippet of app.js:

var express               = require("express"),
app                   = express(),
bodyParser            = require("body-parser"),
mongoose              = require("mongoose"),
passport              = require("passport"),
localStrategy         = require("passport-local"),
methodOverride        = require("method-override"),
flash                 = require("connect-flash"),
session               = require("express-session"),
campground            = require("./models/campground"),
comment               = require("./models/comments"),  //name of the model is comment
User                  = require("./models/user"),
seedDB                = require("./seeds");

var campgroundRoutes = require("./routers/campground"),
commentRoutes    = require("./routers/comment"),
authRoutes       = require("./routers/auth");

var url = process.env.DATABASE_URL || "mongodb://localhost:27017/yelp_camp"; 
mongoose.connect(url, { useNewUrlParser: true });
mongoose.set("useFindAndModify", false);
mongoose.set("useCreateIndex", true);

What am I missing in my code?

Upvotes: 1

Views: 1601

Answers (1)

Vishal-L
Vishal-L

Reputation: 1347

It's always a good practice to handle the promise returned.

var url = process.env.DATABASE_URL || "mongodb://localhost:27017/yelp_camp"; 
mongoose.connect(url, { useNewUrlParser: true })
    .then(() => console.log("Connection Successful"))
    .catch(err => console.log(err));

So this will avoid the UnhandledPromiseRejectionWarning and also you will know the reason why the connection failed. Most probably the issue is with URL.

Upvotes: 3

Related Questions