由于URI错误而无法连接到MongoDB [英] Cannot connect to MongoDB because of wrong URI
本文介绍了由于URI错误而无法连接到MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在节点服务器上运行mongoDB完整的代码来自MongoDB :
我的mongo版本:4.4.3
节点版本:v15.7.0
我已经从MongoDB导入了入门代码,这是代码:
const {MongoClient} = require("mongodb");//连接URIconst uri ="mongodb + srv://sample-hostname:27017/?poolSize = 20&writeConcern = majority";//创建一个新的MongoClientconst client = new MongoClient(uri);异步功能run(){尝试 {//将客户端连接到服务器等待client.connect();//建立并验证连接等待client.db("admin").command({ping:1});console.log(成功连接到服务器");} 最后 {//确保当您完成/出错时客户端将关闭等待client.close();}}run().catch(console.dir);
在终端上,当我运行"node app.js"时,它引发以下错误:
>(节点:79653)警告:正在访问以下项的不存在的属性"MongoError">模块在循环依赖关系中导出(使用`node --trace-warnings>...`以显示在何处创建警告)MongoParseError:URI确实>没有主机名,域名和顶级域名>在parseSrvConnectionString(/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/core/uri_parser.js:50:21)>在parseConnectionString(/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/core/uri_parser.js:594:12)>在连接时(/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/operations/connect.js:284:3)>在/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/mongo_client.js:225:5>在也许Promise(/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/utils.js:681:3)>在MongoClient.connect(/home/harmony/Desktop/FruitsProject/node_modules/mongodb/lib/mongo_client.js:221:10)>运行时(/home/harmony/Desktop/FruitsProject/app.js:12:18)>< anonymous>.(/home/harmony/Desktop/FruitsProject/app.js:21:1)
解决方案
错误正在访问>的不存在的属性'MongoError'循环依赖性内的模块导出
是由 mongodb 3.6.4 中的错误引起的>
已经报告了Full Code here from MongoDB:
My mongo version: 4.4.3
Node version: v15.7.0
I've imported get started code from MongoDB, and here's the code:
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);
On terminal, when i run "node app.js", it throws me following error:
> (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)
解决方案
The error Accessing non-existent property 'MongoError' of > module exports inside circular dependency
is caused by a bug in mongodb 3.6.4
It's already reported here
Back to version 3.6.3 works for me:
npm uninstall mongodb --save
Install version 3.6.3
npm i mongodb@3.6.3
这篇关于由于URI错误而无法连接到MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文