MongoDB Atlas 连接无法在带有 db.js 文件的 Node.js 中工作

问题描述 投票:0回答:1

问题:

  • 当我运行
    mernapp
    应用程序时,我看到它已成功编译的消息,但我遇到了 MongoDB Atlas 连接问题。

日志:

附加信息:

  • 应用程序成功编译并启动,如以下消息所示:

    mathematica
    

    复制代码

    Compiled successfully! You can now view mernapp in the browser. webpack compiled successfully

  • 但是,应用程序未连接到 MongoDB Atlas,并且我没有看到该连接尝试的任何日志。

  • 我已确保在

    db.js
    文件中正确设置了数据库连接,但尚未建立连接。

package.json

{
  "name": "backend",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {     
  "test": "echo \"Error: no test specified\" && exit 1"
},

  "author": "Deepika Goyal",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "express": "^4.21.0",
    "mongoose": "^5.13.19",
    "nodemon": "^3.1.7"
  }
}

index.js

const express = require('express');
const app = express();
const port = 3000;
const mongoDB = require("./db");

console.log("Starting the application...");
mongoDB();

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

db.js

const mongoose = require('mongoose');

// Ensure the URI is in the correct format
const mongoURI = 'mongodb://QuickBites:Deepi%[email protected]:27017,cluster0-shard-00-01.u6hkh.mongodb.net:27017,cluster0-shard-00-02.u6hkh.mongodb.net:27017/?replicaSet=atlas-5u4xbe-shard-0&ssl=true&authSource=admin';

const mongoDB = async () => {
    try {
        console.log("Attempting to connect to MongoDB...");
        await mongoose.connect(mongoURI, {
            useNewUrlParser: true,
            useUnifiedTopology: true
        });
        console.log("Successfully connected to MongoDB");
        const fetched_data = await mongoose.connection.db.collection("items");
        fetched_data.find({}).toArray(function(err, data) {
            if (err) console.log("Error fetching data:", err);
            else console.log("Fetched data:", data);
        });
    } catch (error) {
        console.error("Error connecting to MongoDB:", error.message); // More descriptive error message
    }
};

module.exports = mongoDB;

终端输出

Compiled successfully!

You can now view mernapp in the browser.
Compiled successfully!

You can now view mernapp in the browser.

  Local:            http://localhost:3000
  On Your Network:  http://192.168.56.1:3000

Note that the development build is not optimized.
To create a production build, use npm run build.

webpack compiled successfully

我正在使用

nodemon index.js
运行我的 Node.js Express 应用程序。该应用程序应该在启动时记录
Example app listening on port 5000
,但它没有出现。

我还尝试修复 MongoDB URI,但问题仍然存在。此外,每次我保存文件时,浏览器都会自动打开,这是我没想到的。

mongodb mongoose javascript-objects nodemon
1个回答
0
投票

与 MongoDB Atlas 的连接通常是这样的:

mongodb+srv://QuickBites:Deepi%[email protected]/

您应该立即更改您的密码,我可以使用管理员权限连接到您的数据库!

> mongosh -quiet "mongodb+srv://QuickBites:Deepi%[email protected]/"

Atlas atlas-5u4xbe-shard-0 [primary] test> db.runCommand({connectionStatus : 1}).authInfo
{
  authenticatedUsers: [ { user: 'QuickBites', db: 'admin' } ],
  authenticatedUserRoles: [ { role: 'atlasAdmin', db: 'admin' } ]
}
Atlas atlas-5u4xbe-shard-0 [primary] test>
© www.soinside.com 2019 - 2024. All rights reserved.