[英]when i try to connect to mongodb get this error
Here is my NodeJS code这是我的 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}`); });
This is the .env
file:这是
.env
文件:
ATLAS_URI = mongodb+srv://xxxxxx:xxxxxx@cluster0.jnk3w.mongodb.net/xxxxxx?retryWrites=true&w=majority
This is the error log这是错误日志
(node:3812) UnhandledPromiseRejectionWarning: MongoParseError: option usecreateindex is not supported at Object.parseOptions (D:\\Front_end\\MERN_STACK\\Trackerapp\\tracker\\backend\\node_modules\\mongodb\\lib\\connection_string.js:281:15)
(节点: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)
(Use
node --trace-warnings ...
to show where the warning was created) (node:3812) UnhandledPromiseRejectionWarning: Unhandled promise rejection.(使用
node --trace-warnings ...
显示警告的创建位置)(节点:3812) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。 This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().这个错误要么是因为在没有 catch 块的情况下抛出了异步函数,要么是因为拒绝了一个没有用 .catch() 处理过的承诺。 To terminate the node process on unhandled promise rejection, use the CLI flag
--unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode ).要在未处理的承诺拒绝时终止节点进程,请使用 CLI 标志
--unhandled-rejections=strict
(请参阅https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode )。 (rejection id: 2) (node:3812) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated.(rejection id: 2) (node:3812) [DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。 In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
将来,未处理的承诺拒绝将使用非零退出代码终止 Node.js 进程。
现在在版本> 6 的猫鼬你不要添加useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true,
你可以简单地写{mongoose.connect(uri, {});}
useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true,
{mongoose.connect(uri, {});}
As of version 6.x of mongoose, it is no longer necessary to specify useNewUrlParser: true
and useCreateIndex: true
.从
useNewUrlParser: true
6.x 版本开始,不再需要指定useNewUrlParser: true
和useCreateIndex: true
。 You can simply leave those options out, your code should work then.您可以简单地忽略这些选项,然后您的代码应该可以工作。
You can read more about it in the mongoose v6.x migration guide .您可以在 mongoose v6.x 迁移指南 中阅读有关它的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.