[英]nodeJs - Cannot connect to MongoDB
I'm working on a little blockChain prototype just to learn it myself and the later work on a bigger project.我正在研究一个小区块链原型,只是为了自己学习它,然后再做一个更大的项目。 I've downloaded MongoDB and the "mongod" command Works when I type it in the CMD .
我已经下载了 MongoDB 和“mongod”命令当我在 CMD 中键入它时工作。
However when trying to start my project where the connection is taking place, I'm only getting the error back, that I can't connect to the Mongo DB.但是,当尝试在发生连接的地方启动我的项目时,我只会收到错误消息,即我无法连接到 Mongo DB。
Here's what I got so far:这是我到目前为止得到的:
//Connect to DB
mongoose.connect("mongodb://localhost:27017", (err) => {
if(err)
return console.log("Cannot connect to DB");
console.log("Database is Connected ", err);
connectionCallback();
});
In the MongoDB Compass, I haven't created anything yet.在 MongoDB 指南针中,我还没有创建任何东西。 That's why I don't have any DB in the code snippet after the
"mongodb://localhost:27017
. I've already tried it, while specifying a DB in the connection String.这就是为什么我在
"mongodb://localhost:27017
之后的代码片段中没有任何数据库。我已经尝试过了,同时在连接字符串中指定了一个数据库。
Try changing the URI from mongodb://localhost:27017
to mongodb://127.0.0.1:27017
.尝试将URI从
mongodb://localhost:27017
更改为mongodb://127.0.0.1:27017
。 This might work.这可能会奏效。
why don't you try mongodb library in nodejs to create a mongodb client为什么不尝试在 nodejs 中创建 mongodb 客户端的 mongodb 库
npm install mongodb
this will install it and to connect through the nodejs you can use the followig lines of code这将安装它并通过nodejs进行连接,您可以使用以下代码行
const uri = "mongodb+srv://<username>:<password>@<your-cluster-url>/test?retryWrites=true&w=majority";
you can use this line to connect to the db and create a client您可以使用此行连接到数据库并创建客户端
const client = new MongoClient(uri);
you can also refer here for more details: visit here您也可以参考这里了解更多详情: 访问这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.