连接 ECONNREFUSED ::1:27017

Bug*_*ugi 1 mongodb node.js

您好,我整整两天都在试图解决这个错误,谷歌搜索,阅读这里的相关论坛,但似乎我无法解决这个错误以使我的代码完全发挥作用。我将不胜感激任何帮助

\n

第一位代表错误的描述,后面的位是代码。再次预先感谢您。

\n
[nodemon] restarting due to changes...\n[nodemon] starting `node app.js`\nC:\\Users\\\xeb\xa5\x98 \xea\xb4\x91\xec\x84\xad\\Desktop\\00-starting-project\\node_modules\\mongodb\\lib\\sdam\\topology.js:291\n                const timeoutError = new error_1.MongoServerSelectionError(`Server selection timed out after ${serverSelectionTimeoutMS} ms`, this.description);\n                                     ^\n\nMongoServerSelectionError: connect ECONNREFUSED ::1:27017\n    at Timeout._onTimeout (C:\\Users\\\xeb\xa5\x98 \xea\xb4\x91\xec\x84\xad\\Desktop\\00-starting-project\\node_modules\\mongodb\\lib\\sdam\\topology.js:291:38)\n    at listOnTimeout (node:internal/timers:564:17)\n    at process.processTimers (node:internal/timers:507:7) {\n  reason: TopologyDescription {\n    type: 'Unknown',\n    servers: Map(1) {\n      'localhost:27017' => ServerDescription {\n        address: 'localhost:27017',\n        type: 'Unknown',\n        hosts: [],\n        passives: [],\n        arbiters: [],\n        tags: {},\n        minWireVersion: 0,\n        maxWireVersion: 0,\n        roundTripTime: -1,\n        lastUpdateTime: 50395518,\n        lastWriteDate: 0,\n        error: MongoNetworkError: connect ECONNREFUSED ::1:27017\n            at connectionFailureError (C:\\Users\\\xeb\xa5\x98 \xea\xb4\x91\xec\x84\xad\\Desktop\\00-starting-project\\node_modules\\mongodb\\lib\\cmap\\connect.js:387:20)\n            at Socket.<anonymous> (C:\\Users\\\xeb\xa5\x98 \xea\xb4\x91\xec\x84\xad\\Desktop\\00-starting-project\\node_modules\\mongodb\\lib\\cmap\\connect.js:310:22)\n            at Object.onceWrapper (node:events:628:26)\n            at Socket.emit (node:events:513:28)\n            at emitErrorNT (node:internal/streams/destroy:151:8)\n            at emitErrorCloseNT (node:internal/streams/destroy:116:3)\n            at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {\n          cause: Error: connect ECONNREFUSED ::1:27017\n              at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1247:16) {\n            errno: -4078,\n            code: 'ECONNREFUSED',\n            syscall: 'connect',\n            address: '::1',\n            port: 27017\n          },\n          [Symbol(errorLabels)]: Set(1) { 'ResetPool' }\n        },\n        topologyVersion: null,\n        setName: null,\n        setVersion: null,\n        electionId: null,\n        logicalSessionTimeoutMinutes: null,\n        primary: null,\n        me: null,\n        '$clusterTime': null\n      }\n    },\n    stale: false,\n    compatible: true,\n    heartbeatFrequencyMS: 10000,\n    localThresholdMS: 15,\n    setName: null,\n    maxElectionId: null,\n    maxSetVersion: null,\n    commonWireVersion: 0,\n    logicalSessionTimeoutMinutes: null\n  },\n  code: undefined,\n  [Symbol(errorLabels)]: Set(0) {}\n}\n
Run Code Online (Sandbox Code Playgroud)\n
app.js\n\nconst path = require('path');\n\nconst express = require('express');\n\nconst blogRoutes = require('./routes/blog');\n\nconst app = express();\n\nconst db = require("./data/database");\n\n// Activate EJS view engine\n\napp.set('view engine', 'ejs');\n\napp.set('views', path.join(__dirname, 'views'));\n\napp.use(express.urlencoded({ extended: true })); // Parse incoming request bodies\n\napp.use(express.static('public')); // Serve static files (e.g. CSS files)\n\napp.use(blogRoutes);\n\napp.use(function (error, req, res, next) {\n\n  // Default error handling function\n\n  // Will become active whenever any route / middleware crashes\n\n  console.log(error);\n\n  res.status(500).render('500');\n\n});\n\ndb.connectToDatabase().then(function () {\n\n  app.listen(3000);\n\n});\n
Run Code Online (Sandbox Code Playgroud)\n
database\n\nconst mongodb = require("mongodb");\n\n    const MongoClient = mongodb.MongoClient;\n\n    let database;\n\n    async function connect() {\n\n       const client = await MongoClient.connect("mongodb://localhost:27017");\n\n       database = client.db("blog");\n\n    }\n\n    function getDb() {\n\n        if (!database) {\n\n            throw { message: "Database connection not establisehd!" };\n\n        }\n\n        return database;\n
Run Code Online (Sandbox Code Playgroud)\n

Bug*_*ugi 5

当我输入 127.0.0.1 而不是 localhost 时,问题就解决了。感谢你所做的一切!!!

 async function connect() {
       const client = await MongoClient.connect("mongodb://127.0.0.1:27017"); 
       database = client.db("blog"); 
Run Code Online (Sandbox Code Playgroud)