Mongodb not connecting

The last project I completed on glitch successful, my first serious error was connecting mongodb. Which was later solved by the glitch members,but I ask him the cause of that he never told me,he said its might be some js inside. I never understand and he left, now am on another project and the error again from the console
error: (node:251) UnhandledPromiseRejectionWarning: 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.
PLEASE WILL YOU TELL ME THE CAUSE EVEN AFTER SOLVING THE PROBLEM FOR NEXT TIME SAKE.THANKS

hey, i’ve gotten that error before and it could be a few things:

  1. Install the dotenv package and add this to the top your server.js file:
    const dotenv = require(‘dotenv’)
    dotenv.config({ path: ‘./.env’ })

  2. Make sure your process.env matches up with what you have in the .env file.

  3. I tend to have better luck with promises than async/await sometimes, so if you’re still having issues, maybe try this:

mongoose
.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
.then(() => console.log(‘MongoDB connected!’))
.catch(err => console.log(err))

1 Like