gpt4 book ai didi

javascript - 无法在 Node JS 应用程序中连接 MongoDB

转载 作者:太空宇宙 更新时间:2023-11-04 02:44:57 25 4
gpt4 key购买 nike

我的应用程序运行良好,并在开发一个简单的 Node js 应用程序时连接到 mongoDB。但突然它开始显示这个错误。这是控制台的快照:

enter image description here

这是我提供 mongoURI 的文件,在出现错误之前它工作得很好:

module.exports = {
mongoURI:
"mongodb+srv://glorious:glorious@cluster0-to57n.mongodb.net/test?retryWrites=true&w=majority"
};

这是我的 server.js 文件:

const express = require("express");
const mongoose = require("mongoose");

const app = express();

// DB Config
const db = require("./config/keys").mongoURI;

// Connect to MongoDB
mongoose
.connect(db)
.then(() => console.log("MongoDB Connected"))
.catch(err => console.log(err));

app.get("/", (req, res) => res.send("Hello World"));

const port = process.env.PORT || 5000;

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

这是我的 package.json 文件:

{
"name": "devconnector",
"version": "1.0.0",
"description": "A social network for developers",
"main": "server.js",
"scripts": {
"start": "node server.js",
"server": "nodemon server.js"
},
"author": "Utkarsh Shrivastava",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"gravatar": "^1.8.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.7.13",
"passport": "^0.4.0",
"passport-jwt": "^4.0.0",
"validator": "^12.1.0"
},
"devDependencies": {
"nodemon": "^2.0.1"
}
}

最佳答案

useNewUrlParser doc :

The useNewUrlParser Option

By default, mongoose.connect() will print out the below warning:

DeprecationWarning: current URL string parser is deprecated, and will
be removed in a future version. To use the new parser, pass option {
useNewUrlParser: true } to MongoClient.connect.

The MongoDB Node.js driver rewrote the tool it uses to parse MongoDB connection strings. Because this is such a big change, they put the new connection string parser behind a flag. To turn on this option, pass the useNewUrlParser option to mongoose.connect() or mongoose.createConnection().

mongoose.connect(uri, { useNewUrlParser: true });
mongoose.createConnection(uri, { useNewUrlParser: true });

You can also set the global useNewUrlParser option to turn on useNewUrlParser for every connection by default.

// Optional. Use this if you create a lot of connections and don't
want // to copy/paste `{ useNewUrlParser: true }`.
mongoose.set('useNewUrlParser', true);

To test your app with {useNewUrlParser: true }, you only need to check whether your app successfully connects. Once Mongoose has successfully connected, the URL parser is no longer important. If you can't connect with { useNewUrlParser: true }, please open an issue on GitHub.

useUnifiedTopology doc :

useUnifiedTopology

By default, mongoose.connect() will print out the below warning:

DeprecationWarning: current Server Discovery and Monitoring engine is
deprecated, and will be removed in a future version. To use the new
Server Discover and Monitoring engine, pass option {
useUnifiedTopology: true } to the MongoClient constructor.

Mongoose 5.7 uses MongoDB driver 3.3.x, which introduced a significant refactor of how it handles monitoring all the servers in a replica set or sharded cluster. In MongoDB parlance, this is known as server discovery and monitoring.

To opt in to using the new topology engine, use the below line:

mongoose.set('useUnifiedTopology', true); 

If you find any unexpected behavior, please open up an issue on GitHub.

因此您需要将两个选项传递给 mongoose.connect:

// Connect to MongoDB
mongoose
.connect(db, { useNewUrlParser: true, useUnifiedTopology: true })
...

关于javascript - 无法在 Node JS 应用程序中连接 MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59210440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com