0

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!

Paradox_0
  • 3
  • 4
  • Are both mongod & your npm app running on same host? – harshavmb Mar 19 '22 at 14:50
  • Yo! thank you for responding so fast! I have no idea, how do I check that? – Paradox_0 Mar 19 '22 at 14:56
  • From the error it seems, npm app seems to be connecting to mongod on the same host ie., localhost. If mongod process isn’t running on 27017 port, you get connection refused. If it’s *nix, system you can check whether mongod process is running with “top” or “ps -eaf | grep mongod” & also the port with “netstat -tlnp” commands – harshavmb Mar 19 '22 at 15:12
  • I'm very new to this and a bit confused so please excuse my dumb questions: Do I just type those things directly into the command line? (And I'm using Windows 10.) – Paradox_0 Mar 19 '22 at 15:21
  • Hey, I uploaded a screenshot that show that in compass, the host is localhost: 27017. I think that that means that mongod is running on port 27017, is that correct? – Paradox_0 Mar 20 '22 at 07:57
  • Looks like its connecting to mongod using ipv6 rather ipv4.. `MongoNetworkError: connect ECONNREFUSED ::1:27017`. Any chance you tell mongoose to connect with ipv4 ie., `127.0.0.1` – harshavmb Mar 20 '22 at 16:36
  • YOU FIXED IT! THANK YOU SO MUCH!!!!! It runs like a charm! I replaced the first argument in mongoose.connect to: `"mongodb://127.0.0.1"` 1,000 TIMES THANK YOU! – Paradox_0 Mar 21 '22 at 04:27
  • Glad to help. I’ve added answer now. Accept if it worked.. – harshavmb Mar 21 '22 at 06:03

1 Answers1

0

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

harshavmb
  • 3,404
  • 3
  • 21
  • 55