【问题标题】:Error: queryTxt ETIMEOUT cluster0.dxzhf.mongodb.net错误:queryTxt ETIMEOUT cluster0.dxzhf.mongodb.net
【发布时间】:2022-01-22 20:08:32
【问题描述】:

我正在尝试 npm 启动我的后端,但连接到 mongodb atlas 时出现错误。 这是我的 index.js 代码:

import express from 'express';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import cors from 'cors';

const app = express();

app.use(bodyParser.json({limit: "30mb", extended: true}));
app.use(bodyParser.urlencoded({limit: "30mb", extended: true}));
app.use(cors());

const CONNECTION_URL = "mongodb+srv://Danishbukhari:(mypassword)@cluster0.dxzhf.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"
const PORT = process.env.PORT || 5000;

mongoose.connect(CONNECTION_URL, { useNewUrlParser: true})
.then(() => app.listen(PORT, () => console.log(`Server running on port: ${PORT}`)))
.catch((error) => console.log(error.message) )

我的 package.json 就像:

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "nodemon index.js"
  },
  "author": "Danish",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.1",
    "cors": "^2.8.5",
    "express": "^4.17.2",
    "mongodb": "^4.2.2",
    "mongoose": "^6.1.2",
    "nodemon": "^2.0.15"
  }
}

我也将我的 IP 地址列入了白名单 请告诉我如何解决这个问题:(

【问题讨论】:

    标签: node.js mongodb mongoose mongodb-atlas


    【解决方案1】:

    我遇到了这个问题,将 DNS 更改为 8.8.8.8 对我有用。

    但在此之前,请确保:

    1. 检查您的连接。
    2. 关闭 VPN。
    3. mongoDB中的IP白名单
    4. mongoDB 中检查您的凭据

    祝你好运 ;-) 如果仍有问题,请查看link

    【讨论】:

    • 谢谢老兄。更改 dns 对我没有帮助,但我转到了您提供的链接,然后在阅读解决方案后,我在我的 mongodb 连接页面中将版本更改为 2.2.122 或更高版本,并将新字符串复制粘贴到连接 url 中。这帮助我再次感谢提供该链接。 :)
    猜你喜欢
    • 2022-06-12
    • 1970-01-01
    • 2023-02-18
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    • 2019-10-13
    • 2020-12-16
    • 2021-10-08
    相关资源
    最近更新 更多