0

I recently started working on a script unrelated to the app I've been building and now I can't get my app to connect to the server again. It's been about three weeks since I was in the app and the server connection worked beforehand. I'm not totally sure how long MongoDB stays connected so bringing this up in case it's relevant.

I've tried a bunch of things from stack overflow posts and youtube but can't get my connection to work.

Connection string

const dbUrl = 'mongodb://localhost:27017/macro-tickets';

If I run mongosh in my terminal, I get a connection (below) and noticed that the db route string is different from what I have above...

Current Mongosh Log ID: 64b17475ab3453969945af15
Connecting to:      mongodb://127.0.0.1:27017/? directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.0
Using MongoDB:      6.0.1
Using Mongosh:      1.6.0

Attempted fixes

After noticing the db route is different in the terminal, I changed dbUrl to mongodb://127.0.0.1:27017/macro-tickets, mongodb://0.0.0.0:27017/macro-tickets, mongodb://127.0.0.1/macro-tickets, and mongodb://127.0.0.1/macro-tickets but none work.

I also ran sudo mongod in VSCode's terminal but that didn't help.

I included my full error's below (sorry I know it's a lot of text) for context but any ideas why I can't get this thing to work?

Errors

Error from the client

MongoServerSelectionError: connect ECONNREFUSED ::1:27017
    at Timeout._onTimeout (/Users/myname/Downloads/mtapp/node_modules/connect-mongo/node_modules/mongodb/lib/core/sdam/topology.js:439:30)
    at listOnTimeout (node:internal/timers:564:17)
    at process.processTimers (node:internal/timers:507:7)

Error in my VSCode

MongoServerSelectionError: connect ECONNREFUSED ::1:27017
    at Timeout._onTimeout (/Users/myname/Downloads/mtapp/node_modules/connect-mongo/node_modules/mongodb/lib/core/sdam/topology.js:439:30)
    at listOnTimeout (node:internal/timers:564:17)
    at process.processTimers (node:internal/timers:507:7)
MongoServerSelectionError: connect ECONNREFUSED ::1:27017
    at Timeout._onTimeout (/Users/myname/Downloads/mtapp/node_modules/connect-mongo/node_modules/mongodb/lib/core/sdam/topology.js:439:30)
    at listOnTimeout (node:internal/timers:564:17)
    at process.processTimers (node:internal/timers:507:7)
connection error: MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at Connection.openUri (/Users/myname/Downloads/mtapp/node_modules/mongoose/lib/connection.js:796:32)
    at /Users/myname/Downloads/mtapp/node_modules/mongoose/lib/index.js:328:10
    at /Users/myname/Downloads/mtapp/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/Users/myname/Downloads/mtapp/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (/Users/myname/Downloads/mtapp/node_modules/mongoose/lib/index.js:1149:10)
    at Mongoose.connect (/Users/myname/Downloads/mtapp/node_modules/mongoose/lib/index.js:327:20)
    at Object.<anonymous> (/Users/myname/Downloads/mtapp/app.js:38:10)
    at Module._compile (node:internal/modules/cjs/loader:1218:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1272:10) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) { 'localhost:27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    logicalSessionTimeoutMinutes: undefined
  }
}
node:internal/process/promises:289
            triggerUncaughtException(err, true /* fromPromise */);
            ^

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at Connection.openUri (/Users/myname/Downloads/mtapp/node_modules/mongoose/lib/connection.js:796:32)
    at /Users/myname/Downloads/mtapp/node_modules/mongoose/lib/index.js:328:10
    at /Users/myname/Downloads/mtapp/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/Users/myname/Downloads/mtapp/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (/Users/myname/Downloads/mtapp/node_modules/mongoose/lib/index.js:1149:10)
    at Mongoose.connect (/Users/myname/Downloads/mtapp/node_modules/mongoose/lib/index.js:327:20)
    at Object.<anonymous> (/Users/myname/Downloads/mtapp/app.js:38:10)
    at Module._compile (node:internal/modules/cjs/loader:1218:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1272: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: 166365218,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (/Users/myname/Downloads/mtapp/node_modules/mongodb/lib/cmap/connect.js:293:20)
            at Socket.<anonymous> (/Users/myname/Downloads/mtapp/node_modules/mongodb/lib/cmap/connect.js:267:22)
            at Object.onceWrapper (node:events:628:26)
            at Socket.emit (node:events:513:28)
            at emitErrorNT (node:internal/streams/destroy:151:8)
            at emitErrorCloseNT (node:internal/streams/destroy:116:3)
            at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
          }
        },
        stale: false,
        compatible: true,
        heartbeatFrequencyMS: 10000,
        localThresholdMS: 15,
        logicalSessionTimeoutMinutes: undefined
      }
    }

    Node.js v19.3.0
    [nodemon] app crashed - waiting for file changes before starting...
econobro
  • 315
  • 3
  • 17
  • Thanks for adding this @WernfriedDomscheit. I followed the answer and updated `net: ipv6: true bindIpAll: true` in `nano /usr/local/etc/mongod.conf` but am still receiving the connection error. – econobro Jul 14 '23 at 18:53
  • Is this the config file when mongod is starting? – Wernfried Domscheit Jul 14 '23 at 19:24
  • I restarted my system and above seems to have worked @WernfriedDomscheit. I think it was from your answer since I didn't make any other material changes. I've upvoted your comment and would consider that the answer. Thank you for the help! – econobro Jul 14 '23 at 20:04

0 Answers0