javascript - MongooseServerSelectionError:连接 ECONN

我已经尝试了 2 个多小时,现在试图弄清楚这个数据库有什么问题。我已经尝试了一切。从重新安装服务器、重新启动进程、重新启动等等。尝试连接时它一直给我这个错误:

const serverSelectionError = new ServerSelectionError();
                               ^

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at NativeConnection.Connection.openUri (D:\TheShed\MX_\node_modules\mongoose\lib\connection.js:797:32)
    at D:\TheShed\MX_\node_modules\mongoose\lib\index.js:330:10
    at D:\TheShed\MX_\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (D:\TheShed\MX_\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (D:\TheShed\MX_\node_modules\mongoose\lib\index.js:1151:10)
    at Mongoose.connect (D:\TheShed\MX_\node_modules\mongoose\lib\index.js:329:20)
    at module.exports (D:\TheShed\MX_\other\DB\mong.js:4:20)
    at D:\TheShed\MX_\app.js:195:37
    at Object.<anonymous> (D:\TheShed\MX_\app.js:197:3) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) {
      'localhost:27017' => ServerDescription {
        _hostAddress: HostAddress { isIPv6: false, host: 'localhost', port: 27017 },
        address: 'localhost:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 1421094,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (D:\TheShed\MX_\node_modules\mongodb\lib\cmap\connect.js:293:20)
            at Socket.<anonymous> (D:\TheShed\MX_\node_modules\mongodb\lib\cmap\connect.js:267:22)
            at Object.onceWrapper (node:events:510:26)
            at Socket.emit (node:events:390:28)
            at emitErrorNT (node:internal/streams/destroy:164:8)
            at emitErrorCloseNT (node:internal/streams/destroy:129:3)
            at processTicksAndRejections (node:internal/process/task_queues:83:21)
      }
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    logicalSessionTimeoutMinutes: undefined
  }
}

无论我做什么,这个错误都不会解决。 MongoDB 服务器正在运行,我通过执行 >show dbs 进行了检查,它完美地列出了它们。此外,C:/data/db 存在并且也很好。 我该怎么办? 这是我的连接代码:

(async () => {
    await require('./other/DB/mong')();
    console.log("Connected to Database.");
})();

这是 ./other/DB/mong:

var mongoose = require("mongoose");

module.exports = async () => {
    await mongoose.connect('mongodb://localhost:27017/MX', {
        keepAlive: true,
        useNewUrlParser: true,
        useUnifiedTopology: true
    });
    return mongoose;
}

最佳答案

我刚刚在网上找到了一个解决方案, 如果您使用的是最新的 nodejs (v17.x),请尝试将 mongodb url 从 localhost 更新为 127.0.0.1

这对我有用:slightly_smiling_face:

关于javascript - MongooseServerSelectionError:连接 ECONNREFUSED::1:27017 不会得到修复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69763130/

相关文章:

python - python解释器是否隐含地使用了中国余数定理?

r - 从 R 中的数据帧列表中进行子集化

c# - 在C#中,如何使用switch对非常量字符串值进行模式匹配?

java - 如何在实体中正确创建关系

android - 如何在 Room MVVM 架构中实现 Koin 依赖注入(inject)

assembly - x86中的BEXTR指令是如何工作的

javascript - 如何在 sveltekit 应用程序中将菜单项设置为事件状态

azure-devops - Azure DevOps Pipeline NPM 安装任务因 nod

angular - AWS Lambda@Edge Viewer 请求失败,返回 'The body

c++ - 创建一个线程安全的原子计数器