MongoDB集合为什么无法通过连接您的应用程序来连接?

huangapple go评论57阅读模式
英文:

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}`)
})

huangapple
  • 本文由 发表于 2023年3月31日 19:56:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定