mongodbmongoosemerneconnrefused

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 But my mongod service is running


Every time I run npm start in my backend folder, my server runs successfully for a short time and then soon after crashes. I'm getting this error back from the command line:

[nodemon] restarting due to changes...
[nodemon] starting `babel-node ./index.js`
The basketball server is running on port: 3000
C:\users\work\documents\codeprojects\learnmern\basketball\backend\node_modules\mongoose\lib\connection.js:807
  const serverSelectionError = new ServerSelectionError();
                               ^

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at NativeConnection.Connection.openUri (C:\users\work\documents\codeprojects\learnmern\basketball\backend\node_modules\mongoose\lib\connection.js:807:32)
    at C:\users\work\documents\codeprojects\learnmern\basketball\backend\node_modules\mongoose\lib\index.js:342:10
    at C:\users\work\documents\codeprojects\learnmern\basketball\backend\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (C:\users\work\documents\codeprojects\learnmern\basketball\backend\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (C:\users\work\documents\codeprojects\learnmern\basketball\backend\node_modules\mongoose\lib\index.js:1176:10)
    at Mongoose.connect (C:\users\work\documents\codeprojects\learnmern\basketball\backend\node_modules\mongoose\lib\index.js:341:20)
    at Object.<anonymous> (C:/users/work/documents/codeprojects/learnmern/basketball/backend/index.js:10:10)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at loader (C:\Users\Work\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-register\lib\node.js:144:5) {
  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: 258048414,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (C:\users\work\documents\codeprojects\learnmern\basketball\backend\node_modules\mongodb\src\cmap\connect.ts:514:14)
            at Socket.<anonymous> (C:\users\work\documents\codeprojects\learnmern\basketball\backend\node_modules\mongodb\src\cmap\connect.ts:412:16)
            at Object.onceWrapper (node:events:640:26)
            at Socket.emit (node:events:520:28)
            at Socket.emit (node:domain:475:12)
            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
[nodemon] app crashed - waiting for file changes before starting...

Here is an image that may help trouble shooting: screenshot of my index.js file and Mongo Compass

I'm very new to backend web development and am experiencing this error while atempting to follow along to a LINKEDIN LEARNING course on MERN. linked here: VIDEO 3 FROM THIS COURSE and am very grateful to anyone willing to take the time to help me solve my problem, thanks!


Solution

  • From connect ECONNREFUSED ::1:27017, it appears that Mongoose is trying to connect to ipv6 wherein your screenshots say mongod is using ipv4.

    Use ipv4 ie., 127.0.0.1