Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)]: {} }
like image 801
CryptoScroller Avatar asked Jul 13 '18 01:07

CryptoScroller


1 Answers

First check if the variable/env variable can be logged in the console

console.log(db);

If you are executing your application from some other file than your entry point, then add this in that other file

require('dotenv/config');

OR

require('dotenv').config();

OR

any such import(according to the package you are using) in connection file too.

like image 121
Harsh Mandalgi Avatar answered Sep 18 '22 13:09

Harsh Mandalgi