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)]: {} }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With