gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:09:28 24 4
gpt4 key购买 nike

我试图在 Node 服务器上运行 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);
在终端上,当我运行“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 中的错误引起的
已经举报here
返回版本 3.6.3 对我有用:

npm uninstall mongodb --save
安装版本 3.6.3
npm i mongodb@3.6.3

关于javascript - 由于 URI 错误,无法连接到 MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66049860/

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