leon21leon21asked 4 months ago
try to connect MongoDB using mongoose, But get this error,
MongooseError: The `uri` parameter to `openUri()` must be a string,
got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.

1 Answers

codegenlabcodegenlabanswered 4 months ago
follow these step, step1: install dotenv
npm install dotenv
step2: import dotenv
const dotenv = require('dotenv');
step3: create .env file in app folder(ex: config.env  like this) step4:  add mongodb url like this inside the env file,
MONGO_URI=mongodb://{your database name}
step5: finally add this command in your main js file,
dotenv.config( { path : 'config.env'} )