尝试连接到 MONGODB 但未连接

编程入门 行业动态 更新时间:2024-10-04 13:32:30

尝试<a href=https://www.elefans.com/category/jswz/34/1771407.html style=连接到 MONGODB 但未连接"/>

尝试连接到 MONGODB 但未连接

我正在尝试连接到我在 MongoDB 中的数据库,但它没有连接,我正在使用这个代码(index.js)

const express = require('express');
const mongoose = require('mongoose');

let app = null;
let port = null;
let MONGO_URL = null;



const initVar = async () => {
    port = 9000;
    MONGO_URL = "mongodb+srv://mc3453:[email protected]/customer?retryWrites=true&w=majority";
    app = express();
};

mongoose.set('strictQuery', true);
const connectDB = async () => {
    try {
        await mongoose.connect(MONGO_URL, {
            useNewUrlParser: true,
            useUnifiedTopology: true,
        });
        console.log('MongoDB connected');
    } catch (error) {
        console.log(error);
    }
};

const listentoPort = async () => {
    app.listen(port, async () => {
        console.log(`Server is running on port ${port}`);
    });
};

initVar().then(() => {
    listentoPort().then(() => {
        connectDB();
    });
});

连接字符串为:

mongodb+srv://mc3453:<password>@unify.dyadwbb.mongodb/?retryWrites=true&w=majority

我得到的错误是:


Error: queryTxt ETIMEOUT unify.dyadwbb.mongodb
    at QueryReqWrap.onresolve [as oncomplete] (node:internal/dns/promises:251:17) {
  errno: undefined,
  code: 'ETIMEOUT',
  syscall: 'queryTxt',
  hostname: 'unify.dyadwbb.mongodb'
}

我什至添加了我的 IP 地址,但每次我切换回该网站时,它都说您的 IP 地址不存在,但我又重新添加了它

我该如何解决这个问题请帮我解决这个问题,我是新手请帮帮我

回答如下:

更多推荐

尝试连接到 MONGODB 但未连接

本文发布于:2024-05-31 02:16:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771168.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   但未   MONGODB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!