0

Please I can't connect to Mongodb, I really do need to help to solve this issue, thanks. I've read many other solutions here in stackoverflow but none is working so I'm thinking if it's my laptop that is the problem

OS: win10

Node version: 18.17.0

mongodb version: 6.0.8

mongosh version: 1.10.3

Here's my deployment code but I'm getting an error

const MongoClient = require("mongodb").MongoClient;
const assert = require("assert");

const url = "mongodb://localhost:27017";

const dbName = "fruitsDB";

const client = new MongoClient(url);



client.connect(function(err) {
    assert.equal(null, err);
    console.log("Connected successfully to server");

    const db = client.db(dbName);

        client.close();

});

Here's the error I'm getting.

enter code here

Augustjnr@DESKTOP-649TGK2 MINGW64 ~/desktop/WEB-DEV/fruitProject
$ node app.js
C:\Users\Augustjnr\desktop\WEB-DEV\fruitProject\node_modules\mongodb\lib\sdam\topology.js:278
                const timeoutError = new error_1.MongoServerSelectionError(`Server selection timed out after ${serverSelectionTimeoutMS} ms`, this.description);
                                     ^

MongoServerSelectionError: connect ECONNREFUSED ::1:27017
    at Timeout._onTimeout (C:\Users\Augustjnr\desktop\WEB-DEV\fruitProject\node_modules\mongodb\lib\sdam\topology.js:278:38)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) {
      'localhost:27017' => ServerDescription {
        address: 'localhost:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 21002436,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (C:\Users\Augustjnr\desktop\WEB-DEV\fruitProject\node_modules\mongodb\lib\cmap\connect.js:367:20)
            at Socket.<anonymous> (C:\Users\Augustjnr\desktop\WEB-DEV\fruitProject\node_modules\mongodb\lib\cmap\connect.js:290:22)
            at Object.onceWrapper (node:events:629:26)
            at Socket.emit (node:events:514: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) {
          cause: Error: connect ECONNREFUSED ::1:27017
              at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16) {
            errno: -4078,
            code: 'ECONNREFUSED',
            syscall: 'connect',
            address: '::1',
            port: 27017
          },
          [Symbol(errorLabels)]: Set(1) { 'ResetPool' }
        },
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        primary: null,
        me: null,
        '$clusterTime': null
      }
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: null,
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined,
  [Symbol(errorLabels)]: Set(0) {}
}

Node.js v18.17.0

Also note that the mongodb is working well from my hyper terminal, but if I want to deploy it then it won't work

  • 2
    Please don't use images for code/errors/tracebacks/etc. - just paste them into a code block. – rickhg12hs Aug 18 '23 at 12:01
  • 1
    What error are you getting? It's best to show the full error message. – rickhg12hs Aug 18 '23 at 12:02
  • What is the error you get? You mix mongo shell `monjgosh` and mongoose, where is your problem? The configuration file `authorization: enabled` does not match the log output `Access control is not enabled for this database`, the `dbPath` is also not matching. According log, the MongoDB is running fine. Sorry, your entire question is a mess. – Wernfried Domscheit Aug 18 '23 at 14:24
  • Please don't paste screenshots (or even photos from the screen), use formatted text, see https://meta.stackoverflow.com/q/285551 – Wernfried Domscheit Aug 18 '23 at 14:25
  • Please @Wernfried, I'm new to web development I'm taking online course for it. But the problem I have is that each time I want to connect to the mongodb I get the error connect MongoServerSelectionError: ECONNECTREFUSED I've read things online to solve it but it's not working – Jesus Edward Aug 18 '23 at 19:40
  • You need to provide more information and consistent code and the full error messages (as edit in the question, not as comment) Maybe start with a simple setup of standalone MongoDB before you try to connect it with a webservice. – Wernfried Domscheit Aug 18 '23 at 19:45
  • Please I'm new to stack overflow so I don't really know how to go around doing things here but I'll find away. But please I'm using mongodb 6 and mongoshell 1.8 can you help me on how to connect to the database? – Jesus Edward Aug 18 '23 at 19:49
  • Again, please edit your question (there is a little "Edit" at the bottom) and provide your code as formatted text. Provide the full error message. Explain what you did and limit your question only to one problem, don't put multiple issues in it. Maybe check https://stackoverflow.com/help/how-to-ask – Wernfried Domscheit Aug 19 '23 at 06:10
  • Just a note, none of your pictures shows a mongo shell `mongosh` so it is really difficult to understand your problem. – Wernfried Domscheit Aug 19 '23 at 06:33
  • See this one: https://stackoverflow.com/questions/69840504/mongooseserverselectionerror-connect-econnrefused-127017/75236468#75236468 – Wernfried Domscheit Aug 23 '23 at 05:59

0 Answers0