Mongodb connection failed in my node js web service

Hello I get this error whenever i tried to connect to my mongodb atlas from one of my web services hosted on render which I don’t encounter on other web services and on my local machine as well.

server running on port 4500
Jun 6 12:52:36 AM connected to database
Jun 6 12:53:01 AM /opt/render/project/src/node_modules/mongodb/lib/sdam/topology.js:278
Jun 6 12:53:01 AM const timeoutError = new error_1.MongoServerSelectionError(Server selection timed out after ${serverSelectionTimeoutMS} ms, this.description);
Jun 6 12:53:01 AM ^
Jun 6 12:53:01 AM
Jun 6 12:53:01 AM MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
Jun 6 12:53:01 AM at Timeout._onTimeout (/opt/render/project/src/node_modules/mongodb/lib/sdam/topology.js:278:38)
Jun 6 12:53:01 AM at listOnTimeout (node:internal/timers:557:17)
Jun 6 12:53:01 AM at processTimers (node:internal/timers:500:7) {
Jun 6 12:53:01 AM reason: TopologyDescription {
Jun 6 12:53:01 AM type: ‘Unknown’,
Jun 6 12:53:01 AM servers: Map(1) {
Jun 6 12:53:01 AM ‘localhost:27017’ => ServerDescription {
Jun 6 12:53:01 AM address: ‘localhost:27017’,
Jun 6 12:53:01 AM type: ‘Unknown’,
Jun 6 12:53:01 AM hosts: ,
Jun 6 12:53:01 AM passives: ,
Jun 6 12:53:01 AM arbiters: ,
Jun 6 12:53:01 AM tags: {},
Jun 6 12:53:01 AM minWireVersion: 0,
Jun 6 12:53:01 AM maxWireVersion: 0,
Jun 6 12:53:01 AM roundTripTime: -1,
Jun 6 12:53:01 AM lastUpdateTime: 482690819,
Jun 6 12:53:01 AM lastWriteDate: 0,
Jun 6 12:53:01 AM error: MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
Jun 6 12:53:01 AM at connectionFailureError (/opt/render/project/src/node_modules/mongodb/lib/cmap/connect.js:370:20)
Jun 6 12:53:01 AM at Socket. (/opt/render/project/src/node_modules/mongodb/lib/cmap/connect.js:293:22)
Jun 6 12:53:01 AM at Object.onceWrapper (node:events:510:26)
Jun 6 12:53:01 AM at Socket.emit (node:events:390:28)
Jun 6 12:53:01 AM at emitErrorNT (node:internal/streams/destroy:157:8)
Jun 6 12:53:01 AM at emitErrorCloseNT (node:internal/streams/destroy:122:3)
Jun 6 12:53:01 AM at processTicksAndRejections (node:internal/process/task_queues:83:21) {
Jun 6 12:53:01 AM cause: Error: connect ECONNREFUSED 127.0.0.1:27017
Jun 6 12:53:01 AM at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16) {
Jun 6 12:53:01 AM errno: -111,
Jun 6 12:53:01 AM code: ‘ECONNREFUSED’,
Jun 6 12:53:01 AM syscall: ‘connect’,
Jun 6 12:53:01 AM address: ‘127.0.0.1’,
Jun 6 12:53:01 AM port: 27017
Jun 6 12:53:01 AM },
Jun 6 12:53:01 AM [Symbol(errorLabels)]: Set(1) { ‘ResetPool’ }
Jun 6 12:53:01 AM },
Jun 6 12:53:01 AM topologyVersion: null,
Jun 6 12:53:01 AM setName: null,
Jun 6 12:53:01 AM setVersion: null,
Jun 6 12:53:01 AM electionId: null,
Jun 6 12:53:01 AM logicalSessionTimeoutMinutes: null,
Jun 6 12:53:01 AM primary: null,
Jun 6 12:53:01 AM me: null,
Jun 6 12:53:01 AM ‘$clusterTime’: null
Jun 6 12:53:01 AM }
Jun 6 12:53:01 AM },
Jun 6 12:53:01 AM stale: false,
Jun 6 12:53:01 AM compatible: true,
Jun 6 12:53:01 AM heartbeatFrequencyMS: 10000,
Jun 6 12:53:01 AM localThresholdMS: 15,
Jun 6 12:53:01 AM setName: null,
Jun 6 12:53:01 AM maxElectionId: null,
Jun 6 12:53:01 AM maxSetVersion: null,
Jun 6 12:53:01 AM commonWireVersion: 0,
Jun 6 12:53:01 AM logicalSessionTimeoutMinutes: null
Jun 6 12:53:01 AM },
Jun 6 12:53:01 AM code: undefined,
Jun 6 12:53:01 AM [Symbol(errorLabels)]: Set(0) {}
Jun 6 12:53:01 AM }

1 Like

I have the same problem!

Hi Talibeh,

As you can see in the error logs provided, your application is trying to connect to the database at 127.0.0.1:27017

Check your code to see what value it’s using the connect to your database.

Usually, you’ll set a DATABASE_URL for your MongoDB atlas instance as an environment variable, then use that in your code.

Regards,

Matt

1 Like

Ohh sorry, I solved mine by updating the database i want to connect to on my connectDatabase middleware function

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.