繁体   English   中英

当我尝试连接到 mongodb 时出现此错误

[英]when i try to connect to mongodb get this error

这是我的 NodeJS 代码

const express = require('express'); 
const cors = require('cors'); 
const mongoose = require('mongoose'); 
require('dotenv').config(); 
const app = express(); 
const port = process.env.PORT || 6000; 
app.use(cors()); 
app.use(express.json()); 

const uri = process.env.ATLAS_URI; 
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true } ); 

const connection = mongoose.connection; 
connection.once('open', () => { 
    console.log("MongoDB database connection established successfully"); 
}) 

app.listen(port, () => { console.log(`Server is running on port: ${port}`); });

这是.env文件:

ATLAS_URI = mongodb+srv://xxxxxx:xxxxxx@cluster0.jnk3w.mongodb.net/xxxxxx?retryWrites=true&w=majority

这是错误日志

(节点:3812)未处理的PromiseRejectionWarning:MongoParseError:Object.parseOptions不支持选项usecreateindex(D:\\Front_end\\MERN_STACK\\Trackerapp\\tracker\\backend\\node_modules\\mongodb\\lib\\connection_string.js:281:15)

 at new MongoClient (D:\\Front_end\\MERN_STACK\\Trackerapp\\tracker\\backend\\node_modules\\mongodb\\lib\\mongo_client.js:62:46) at D:\\Front_end\\MERN_STACK\\Trackerapp\\tracker\\backend\\node_modules\\mongoose\\lib\\connection.js:785:16 at new Promise (<anonymous>) at NativeConnection.Connection.openUri (D:\\Front_end\\MERN_STACK\\Trackerapp\\tracker\\backend\\node_modules\\mongoose\\lib\\connection.js:782:19) at D:\\Front_end\\MERN_STACK\\Trackerapp\\tracker\\backend\\node_modules\\mongoose\\lib\\index.js:330:10 at D:\\Front_end\\MERN_STACK\\Trackerapp\\tracker\\backend\\node_modules\\mongoose\\lib\\helpers\\promiseOrCallback.js:32:5 at new Promise (<anonymous>) at promiseOrCallback (D:\\Front_end\\MERN_STACK\\Trackerapp\\tracker\\backend\\node_modules\\mongoose\\lib\\helpers\\promiseOrCallback.js:31:10) at Mongoose._promiseOrCallback (D:\\Front_end\\MERN_STACK\\Trackerapp\\tracker\\backend\\node_modules\\mongoose\\lib\\index.js:1151:10)

(使用node --trace-warnings ...显示警告的创建位置)(节点:3812) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。 这个错误要么是因为在没有 catch 块的情况下抛出了异步函数,要么是因为拒绝了一个没有用 .catch() 处理过的承诺。 要在未处理的承诺拒绝时终止节点进程,请使用 CLI 标志--unhandled-rejections=strict (请参阅https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode )。 (rejection id: 2) (node:3812) [DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。 将来,未处理的承诺拒绝将使用非零退出代码终止 Node.js 进程。

现在在版本> 6 的猫鼬你不要添加useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true,你可以简单地写{mongoose.connect(uri, {});} useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true, {mongoose.connect(uri, {});}

useNewUrlParser: true 6.x 版本开始,不再需要指定useNewUrlParser: trueuseCreateIndex: true 您可以简单地忽略这些选项,然后您的代码应该可以工作。

您可以在 mongoose v6.x 迁移指南 中阅读有关它的更多信息。

暂无
暂无

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

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