英文:
Why Mongodb collection is not connecting via connect your application?
问题
为什么MongoDB集合无法通过连接您的应用程序进行连接?
英文:
When i try with mongoose ,it connects. But whrn i am tring with connect with your application and mongoclient this code does not work. But this code is in their documnetation. Why Mongodb collection is not connecting via connect your application?
const express = require('express');
const app = express();
const password = "Kr6bdMTskY3rYwF4";
app.get('/', (req, res) => {
res.send('hello i am working')
})
const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = "mongodb+srv://organicUser:Kr6bdMTskY3rYwF4@cluster0.iz1nfji.mongodb.net/?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
client.connect(err => {
const collection = client.db("organicdb").collection("products");
// perform actions on the collection object
console.log("Database connected");
client.close();
});
app.listen(3000);
Why Mongodb collection is not connecting via connect your application?
答案1
得分: 0
您的Express应用程序正在运行,但您忘记了app.listen
函数。您需要为您的应用程序分配一个端口并让其监听该端口。此外,您的Mongodb错误已经解决。
const express = require('express');
const PORT = 3000;
const app = express();
const password = "Kr6bdMTskY3rYwF4";
app.get('/', (req, res) => {
res.send('hello i am working')
})
const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = "mongodb+srv://organicUser:Kr6bdMTskY3rYwF4@cluster0.iz1nfji.mongodb.net/?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
async function run() {
try {
const database = client.db("organicdb");
const collection = database.collection("products");
// perform actions on the collection object
console.log(collection);
} finally {
await client.close();
}
}
run().catch(console.dir);
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`)
})
英文:
Your Express application is working but you forgot the app.listen function. You need to assign a port to your app and make it listen to that port. Also your Mongodb error is solved.
const express = require('express');
const PORT = 3000;
const app = express();
const password = "Kr6bdMTskY3rYwF4";
app.get('/', (req, res) => {
res.send('hello i am working')
})
const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = "mongodb+srv://organicUser:Kr6bdMTskY3rYwF4@cluster0.iz1nfji.mongodb.net/?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
async function run() {
try {
const database = client.db("organicdb");
const collection = database.collection("products");
// perform actions on the collection object
console.log(collection);
} finally {
await client.close();
}
}
run().catch(console.dir);
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`)
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论