kakashi96
kakashi96

Reputation: 13

tried to connect mongodb with my app getting an error

The error is "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." I'm attaching ss of the file and the error error image

the code in db.js(suubed in config folder) is

const mongoose = require('mongoose')

const connectDB = async () => {
    try {
        const conn = await mongoose.connect(process.env.MONGO_URI, {
            useNewUrlParser: true,
            useUnifiedTopology: true,
            useFindAndModify: false
        })

        console.log(`${conn.connection.host}`)
    } catch (err) {
        console.log(err)
    }
}

module.exports = connectDB

Upvotes: 0

Views: 28

Answers (1)

Kipras Melnikovas
Kipras Melnikovas

Reputation: 419

Please don't attach external links and/or screenshots - put the code right here.

As the error says, the "uri" (first parameter) to mongoose.connect is undefined, and needs to be a string -- make sure you're loading your environment variables successfully and make sure you didn't make a typo in the process.env.MONGO_URI variable name, since it currently evaluates to undefined.

Upvotes: 0

Related Questions