MongooseServerSelectionError:无法从 Express Server 连接到 MongoDB Atlas

问题描述 投票:0回答:1
const express = require("express");
const app = express();
const cors = require("cors");
const mongoose = require("mongoose");
const dotenv = require("dotenv").config();

const authRoutes = require("./routes/auth");
const PORT = process.env.PORT || 5000;

app.use(cors());
app.use(express.json()); 
app.use(express.urlencoded({ extended: true })); 
app.use(express.static("public")); 

// mongoose
mongoose
  .connect(
    "mongodb+srv://<username:password>@cluster1.1h79s.mongodb.net/?retryWrites=true&w=majority&appName=Cluster1"
  )
  .then(() => {
    console.log("MongoDB cennect");
    app.listen(PORT, () => console.log(`Server Port: ${PORT}`));
  })
  .catch((err) => console.log(`${err} did not connect`));

当我尝试启动 Express 服务器时,出现以下错误:

MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist.

任何有关如何解决此问题的帮助或建议将不胜感激!

我当前在尝试将 Express 服务器连接到 MongoDB Atlas 时遇到问题。我已验证以下内容:

  1. 连接 URI:我使用的是正确的连接字符串。我用我的实际凭据替换了用户名和密码。

  2. IP 白名单:我已将本地 IP 地址和 MongoDB Atlas 安全设置上的 0.0.0.0/0 列入白名单。

  3. MongoDB Compass:我可以使用 MongoDB Compass 连接到 MongoDB Atlas 集群,没有任何问题。

mongodb express
1个回答
0
投票

我也遇到了同样的问题,我使用的是 mongoose 版本 5.13.22 而不是新版本

© www.soinside.com 2019 - 2024. All rights reserved.