英文:
I am trying to connect my mongodb server with node js app but its not working idk why even server mongod is running
问题
const MongoClient = require("mongodb").MongoClient;
const assert = require("assert");
const url = "mongodb://localhost:27017";
const dbName = "fruitsDB";
const client = new MongoClient(url, { useNewUrlParser: true });
client.connect(function (err) {
assert.equal(null, err);
console.log("connected to the server");
const db = client.db(dbName);
client.close();
});
<details>
<summary>英文:</summary>
const MongoClient = require("mongodb").MongoClient;
const assert = require("assert");
const url = "mongodb://localhost:27017";
const dbName = "fruitsDB";
const client = new MongoClient(url, { useNewUrlParser: true });
client.connect(function (err) {
assert.equal(null, err);
console.log("connected to the server");
const db = client.db(dbName);
client.close();
});
[![image description][1]][1]
[1]: https://i.stack.imgur.com/crZXW.png
This code should show in the terminal "connected to the server."
</details>
# 答案1
**得分**: 0
你应该使用 `localhost` IP 地址来连接 MongoDB。
我不知道为什么,但当我尝试使用这个 URL 时它不起作用:```"mongodb://localhost:27017"```
请使用这个:
```use const url = "mongodb://127.0.0.1:27017";```
<details>
<summary>英文:</summary>
You should use `localhost` ip address for connecting to mongodb.
I don't know why but it was not working when i try to do with this url ```"mongodb://localhost:27017"```
Go with this one
```use const url = "mongodb://127.0.0.1:27017";```
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论