0
const timeoutError = new error_1.MongoServerSelectionError(`Server selection timed out after ${serverSelectionTimeoutMS} ms`, this.description);
                                     ^

MongoServerSelectionError: connect ECONNREFUSED ::1:27017
    at Timeout._onTimeout (D:\Coding\web\node_modules\mongodb\lib\sdam\topology.js:293:38)
    at listOnTimeout (node:internal/timers:564:17)
    at process.processTimers (node:internal/timers:507:7) {
  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: 72838933,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (D:\Coding\web\node_modules\mongodb\lib\cmap\connect.js:379:20)
            at Socket.<anonymous> (D:\Coding\web\node_modules\mongodb\lib\cmap\connect.js:302: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) {
          [Symbol(errorLabels)]: Set(0) {}
        }
      }
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    logicalSessionTimeoutMinutes: undefined
  },
  code: undefined,
  [Symbol(errorLabels)]: Set(0) {}
}

Node.js v18.7.0
[nodemon] app crashed - waiting for file changes before starting...

Code is:

const mongoose = require("mongoose");
const MongoUrl = "mongodb://localhost:27017";

const mongoConnect = () => {
 //   mongoose.connect(
 //     MongoUrl,
 //     { useNewUrlParser: true, useUnifiedTopology: true, family: 
          4 },
     //     (err) => {
  //       if (!err) {
    //         console.log("Connected");
    //       } else {
    //         console.log("?Not connected", err);
  //       }
//     }
 //   );
       mongoose.connect(MongoUrl, (err)=>{
       if(!err)
       {
        console.log("Connected");
        }
        else{
        console.log("not");
        }
   })
 };

 mongoConnect();
robertklep
  • 198,204
  • 35
  • 394
  • 381
  • First question will be: are there a MongoDB server running on your machine? Try `mongodb://127.0.0.1` as URL. – robertklep Aug 17 '22 at 05:16

1 Answers1

0

The most probable cause reason of such error would be that mongod might no be active.

If you are using a Linux based OS,

Try this to check if mongodb status:

sudo systemctl status mongod

If running

● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Wed 2020-10-14 14:13:40 UTC; 3s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 1604 (mongod)
     Memory: 210.8M
     CGroup: /system.slice/mongod.service
             └─1604 /usr/bin/mongod --config /etc/mongod.conf

If not running

● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: inactive (dead)
       Docs: https://docs.mongodb.org/manual

To start mongodb

sudo systemctl start mongod

Referenced from : Is mongodb running?

If you are on Windows,

Simply execute this to start MongoDB:

mongo
Krushnal Patel
  • 422
  • 2
  • 14