如何修复mongoose连接node js错误

发布于 2025-01-10 08:08:58 字数 3476 浏览 0 评论 0原文

我确保我的 mongod 正在运行。

我已经从服务中检查了我的 Mongo Db 正在运行,并且还重新启动了很多次。

我尝试使用 127.0.0.1 而不是 localhost

我还启用了 mongod --ipv6。但它仍然给我这些错误。

我已经重新安装了 mongoose 和 Mongo Db。但它仍然抛出错误。

这是我的 index.js 代码

    const mongoose = require('mongoose');
    mongoose.connect('mongodb://localhost:27017/db_name',
    {
    
      useUnifiedTopology: true
     }
    );

,这是我的 json 文件。

    {
     "name": "new-folder",
     "version": "1.0.0",
     "description": "",
     "main": "index.js",
     "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1"
    },
     "author": "",
     "license": "ISC",
     "dependencies": {
     "express": "^4.17.3",
     "mongodb": "^4.4.0",
     "mongoose": "^6.2.3",
     "nodemon": "^2.0.15"
     }
    }

这是我收到的错误。

    E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\connection.js:807
    const serverSelectionError = new ServerSelectionError();
                               ^

     MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at NativeConnection.Connection.openUri (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\connection.js:807:32)    at E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\index.js:340:10
    at E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)    
    at Mongoose._promiseOrCallback (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\index.js:1140:10)
    at Mongoose.connect (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\index.js:339:20)
    at Object.<anonymous> (E:\Web Dev\Backend\New folder\index.js:31:10)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10) {
    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: 292201728,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (E:\Web Dev\Backend\New folder\node_modules\mongoose\node_modules\mongodb\lib\cmap\connect.js:381:20)
            at Socket.<anonymous> (E:\Web Dev\Backend\New folder\node_modules\mongoose\node_modules\mongodb\lib\cmap\connect.js:301:22)
            at Object.onceWrapper (node:events:640:26)
            at Socket.emit (node:events:520: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
     }
    }

    Node.js v17.4.0

I made sure that my mongod is running.

I have checked from services that my Mongo Db is running and also restarted it so many times.

I have tried using 127.0.0.1 instead of localhost

I have also enable the mongod --ipv6.But it still giving me these errors.

I have reinstalled mongoose and Mongo Db.But it still throwing me error.

Here is my index.js code

    const mongoose = require('mongoose');
    mongoose.connect('mongodb://localhost:27017/db_name',
    {
    
      useUnifiedTopology: true
     }
    );

here is my json file.

    {
     "name": "new-folder",
     "version": "1.0.0",
     "description": "",
     "main": "index.js",
     "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1"
    },
     "author": "",
     "license": "ISC",
     "dependencies": {
     "express": "^4.17.3",
     "mongodb": "^4.4.0",
     "mongoose": "^6.2.3",
     "nodemon": "^2.0.15"
     }
    }

Here is the error Im getting.

    E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\connection.js:807
    const serverSelectionError = new ServerSelectionError();
                               ^

     MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at NativeConnection.Connection.openUri (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\connection.js:807:32)    at E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\index.js:340:10
    at E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)    
    at Mongoose._promiseOrCallback (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\index.js:1140:10)
    at Mongoose.connect (E:\Web Dev\Backend\New folder\node_modules\mongoose\lib\index.js:339:20)
    at Object.<anonymous> (E:\Web Dev\Backend\New folder\index.js:31:10)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10) {
    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: 292201728,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (E:\Web Dev\Backend\New folder\node_modules\mongoose\node_modules\mongodb\lib\cmap\connect.js:381:20)
            at Socket.<anonymous> (E:\Web Dev\Backend\New folder\node_modules\mongoose\node_modules\mongodb\lib\cmap\connect.js:301:22)
            at Object.onceWrapper (node:events:640:26)
            at Socket.emit (node:events:520: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
     }
    }

    Node.js v17.4.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

¢蛋碎的人ぎ生 2025-01-17 08:08:58

兄弟不要使用 { useUnifiedTopology: true } 这个命令。而是尝试不使用此代码并向其添加一个函数,如下所示

    if (err) throw err;
    console.log("Database created!");
    db.close();
  }

bro don't use { useUnifiedTopology: true } this command. instead try it without this code and add a function to it like this

    if (err) throw err;
    console.log("Database created!");
    db.close();
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文