由于 URI 错误,无法连接到 MongoDB

Cannot connect to MongoDB because of wrong URI

我试图在节点服务器上 运行 mongoDB Full Code here from MongoDB:

My mongo version: 4.4.3

Node version: v15.7.0

我从 MongoDB 导入了入门代码,代码如下:

const { MongoClient } = require("mongodb");
// Connection URI
const uri =
  "mongodb+srv://sample-hostname:27017/?poolSize=20&writeConcern=majority";
// Create a new MongoClient
const client = new MongoClient(uri);
async function run() {
  try {
    // Connect the client to the server
    await client.connect();
    // Establish and verify connection
    await client.db("admin").command({ ping: 1 });
    console.log("Connected successfully to server");
  } finally {
    // Ensures that the client will close when you finish/error
    await client.close();
  }
}
run().catch(console.dir);

在终端上,当我 运行“节点 app.js”时,它抛出以下错误:

> (node:79653) Warning: Accessing non-existent property 'MongoError' of
> module exports inside circular dependency (Use `node --trace-warnings
> ...` to show where the warning was created) MongoParseError: URI does
> not have hostname, domain name and tld
>     at parseSrvConnectionString (/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/core/uri_parser.js:50:21)
>     at parseConnectionString (/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/core/uri_parser.js:594:12)
>     at connect (/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/operations/connect.js:284:3)
>     at /home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/mongo_client.js:225:5
>     at maybePromise (/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/utils.js:681:3)
>     at MongoClient.connect (/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/mongo_client.js:221:10)
>     at run (/home/harmony/Desktop/FruitsProject/app.js:12:18)
>     at Object.<anonymous> (/home/harmony/Desktop/FruitsProject/app.js:21:1)

错误 Accessing non-existent property 'MongoError' of > module exports inside circular dependency 是由 mongodb 3.6.4

中的错误引起的

已经举报了here

回到版本 3.6.3 适合我:

npm uninstall mongodb --save

安装版本 3.6.3

npm i mongodb@3.6.3

对于搜索此警告的每个人,请不要担心,这只是一个版本错误,并且已经报告。只需卸载 3.6.4 版本并安装 3.6.3 版本,如@kmgt answer 中所述。

更多详情:
https://developer.mongodb.com/community/forums/t/warning-accessing-non-existent-property-mongoerror-of-module-exports-inside-circular-dependency/15411

https://github.com/Automattic/mongoose/issues/9900

我按照建议降级了 MongoDB,但仅此并没有解决问题。

我也必须降级猫鼬才能消除错误。

我降级为:

MongoDB 版本 3.6.3 猫鼬版本 5.11.15

您不必降级您的 MongoDB,只需将您的 mongoose 包降级到 5.11.15,如本文所述: https://github.com/Automattic/mongoose/issues/9900#issuecomment-778535254 我正在使用 Mongo v4.4.3,只是降级 mongoose 对我有用。

我也有类似的错误,我是 运行 在 MacOS Catelina 上:

(node:3265) Warning: Accessing non-existent property 'MongoError' of module exports inside circular dependency (Use `node --trace-warnings ...` to show where the warning was created) Server started on port 3000

之前 运行 于:

node: v14.15.4, mongodb: v4.4.3, mongoose: v5.11.18

将mongoose降级到v5.11.15后,错误消失。

  1. 从终端卸载 mongoose
    npm uninstall mongoose

  2. 安装猫鼬版本 5.11.15
    npm i mongoose@5.11.15

[mongo 错误 1

因为我在图片上用红色矩形显示的线