繁体   English   中英

`openUri()` 的 `uri` 参数必须是字符串,得到“未定义”。 确保 `mongoose.connect()` 或 `mongoose 的第一个参数

[英]The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose

错误显示:

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.

这是代码

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()

连接,连接.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

错误显示如下: openUri()uri参数必须是字符串,得到“未定义”。 确保mongoose.connect()mongoose.createConnection()的第一个参数是一个字符串。

如何解决这个问题?

我得到了解决方案

useNewUrlParser、useUnifiedTopology、useFindAndModify 和 useCreateIndex 不再是受支持的选项。 Mongoose 6 总是表现得好像 useNewUrlParser、useUnifiedTopology 和 useCreateIndex 为 true,而 useFindAndModify 为 false。 请从您的代码中删除这些选项。

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

连接.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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM