mongoose.connect in my express app have name: MongoParserError , message: 'URI malformed, cannot be parsed'

CryptoScroller picture CryptoScroller · Jul 13, 2018 · Viewed 14.3k times · Source
mongoose
    .connect(db,{ useNewUrlParser: true}) 
    .then(() => console.log("MongoDB conected ..."))
    .catch(err => console.log(err));

Above mentioned is my code where 'db' is my database URI provided by mLab

When I write run the express code its catches the error and gives out specific err message as:

URI malformed, cannot be parsed

{ MongoError: URI malformed, cannot be parsed
    at parseConnectionString (/Users/dibs/Desktop/mern-ex/node_modules/mongodb-core/lib/uri_parser.js:207:21)
    at connect (/Users/dibs/Desktop/mern-ex/node_modules/mongodb/lib/operations/mongo_client_ops.js:179:3)
    at connectOp (/Users/dibs/Desktop/mern-ex/node_modules/mongodb/lib/operations/mongo_client_ops.js:283:3)
    at executeOperation (/Users/dibs/Desktop/mern-ex/node_modules/mongodb/lib/utils.js:420:24)
    at MongoClient.connect (/Users/dibs/Desktop/mern-ex/node_modules/mongodb/lib/mongo_client.js:168:10)
    at Promise (/Users/dibs/Desktop/mern-ex/node_modules/mongoose/lib/connection.js:487:12)
    at Promise (<anonymous>)
    at NativeConnection.Connection.openUri (/Users/dibs/Desktop/mern-ex/node_modules/mongoose/lib/connection.js:484:19)
    at Mongoose.connect (/Users/dibs/Desktop/mern-ex/node_modules/mongoose/lib/index.js:229:15)
    at Object.<anonymous> (/Users/dibs/Desktop/mern-ex/server.js:20:6)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Function.Module.runMain (module.js:605:10)
  name: 'MongoParseError',
  message: 'URI malformed, cannot be parsed',
  [Symbol(mongoErrorContextSymbol)]: {} }

Answer

Ali Shirazee picture Ali Shirazee · Apr 12, 2019

Hey I resolved this issue:

Try this, first insure you have .env file with the correct link, then go over to index.js and locate require(“dotenv”).config()

change the name from dotenv to env and the back to dotenv again.

what this does is it forces the MongoDb to restart, I tried using Command + S and it gave me the same err but after changing the values in index.js it works fine. please refer to the image