Attempting to connect to MongoDB via a follow-along, and I keep getting this error:
The
uriparameter toopenUri()must be a string, got "undefined". Make sure the first parameter tomongoose.connect()ormongoose.createConnection()is a string.
Here is the code I’m working with:
.env file (I changed the username and password. The name is cluster0)
MONGO_URI="mongodb+srv://username:password@cluster0.l94wepe.mongodb.net/?retryWrites=true&w=majority"
Server.js
dotenv.config();
mongoose
.connect(process.env.MONGODB_URI)
.then(() => {
console.log("connected to db");
})
.catch((err) => {
console.log(err.message);
});
Any help is appreciated, let me know what other info I can provide. Thank you.
>Solution :
The .env file has "MONGO_URI" and the JS file has "MONGODB_URI"