Home > Mobile >  The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first
The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first

Time:06-23

the error showing :

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.

here are the codes

index.js

const express = require('express');
const app = express();
const task = require('./starter/routes/task')
const connectDB=require('./starter/db/connect')// DB connection

require('dotenv').config()

//middleware


app.use(express.json())


//routers

const PORT = process.env.PORT|| 3000;
const start =async()=>{
    try{   
await connectDB(process.env.MONGO_URI)
// server.listen(3000);
app.listen(PORT,()=>{
    console .log(`server  is connected to port ${(PORT)}...`);  
})   
    }catch(error){
       console.log(error); 
    }
}
start()

connection , connect.js

const mongoose = require('mongoose')

 const connectDB =  async (url)=>{
            return await mongoose.connect(url,{
                //must add in order to not get any error masseges:
                useNewUrlParser: true,
                useCreateIndex: true,
                useFindAndModify: false,
                useUnifiedTopology: true,
            })
          .then(()=>console.log('connected to db...'))  
        .catch((error) => console.log(error.message))
            // process.exit(1) //passing 1 - will exit the proccess with error
   
        }

module.exports=connectDB

the error shows like: 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.

how to fix this?

CodePudding user response:

I got the solution

useNewUrlParser, useUnifiedTopology, useFindAndModify, and useCreateIndex are no longer supported options. Mongoose 6 always behaves as if useNewUrlParser, useUnifiedTopology, and useCreateIndex are true, and useFindAndModify is false. Please remove these options from your code.

https://mongoosejs.com/docs/migrating_to_6.html#no-more-deprecation-warning-options

connect.js

const mongoose = require('mongoose')



const  connectDB =  async(url)=>{
return await mongoose.connect(url)
 .then(()=>console.log('connected to db...'))   
.catch((error)=>console.log(error))

}

module.exports=connectDB
  • Related