Search code examples
mongooseuri

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)]: {} }

Solution

  • I have some problem. please check, that your uri isn't undefined you can use console.log(db)