[英]Cannot connect to MongoDB because of wrong URI
我試圖在 MongoDB 的節點服務器上運行 mongoDB 完整代碼:
我的 mongo 版本:4.4.3
節點版本: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);
在終端上,當我運行“node 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中的錯誤引起的
返回版本 3.6.3 對我有用:
npm uninstall mongodb --save
安裝版本 3.6.3
npm i mongodb@3.6.3
對於搜索此警告的每個人,請不要擔心,這只是一個版本錯誤,並且已經報告了。 只需卸載 3.6.4 版本並安裝 3.6.3 版本,如@kmgt 回答中的回答。
我按照建議降級了 MongoDB 但僅此一項並不能解決問題。
我也不得不降級 mongoose 才能消除錯誤。
我降級為:
MongoDB 版本 3.6.3 Mongoose 版本 5.11.15
You don't have to downgrade your MongoDB, just downgrade your mongoose package to 5.11.15 as in this mention: https://github.com/Automattic/mongoose/issues/9900#issuecomment-778535254 I'm using 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
之前運行於:
節點:v14.15.4,mongodb:v4.4.3,mongoose:v5.11.18
將 mongoose 降級到 v5.11.15 后,錯誤消失了。
從終端卸載 mongoose
npm uninstall mongoose
安裝mongoose版本5.11.15
npm i mongoose@5.11.15
[mongo錯誤1
因為我在圖片上用紅色矩形顯示的那條線
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.