0

When I run my project i got the error.I just learning mongoDB database with nodejs and Express.

POST /submit 200 272.371 ms - 14
GET /submit 404 454.789 ms - 1595
/home/rifan/ExpressSample/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 (/home/rifan/ExpressSample/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: 3614528,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (/home/rifan/ExpressSample/node_modules/mongodb/lib/cmap/connect.js:370:20)
            at Socket.<anonymous> (/home/rifan/ExpressSample/node_modules/mongodb/lib/cmap/connect.js:293: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) {
          cause: Error: connect ECONNREFUSED ::1:27017
              at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
            errno: -111,
            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.16.0

My index.js is..

var express = require('express');
var router = express.Router();
var MongoClient = require('mongodb').MongoClient;

/* GET home page. */
router.get('/', function(req, res, next) {
  const d = new Date();
  const values = ['Samsung', 'Apple', 'Xiaomi', 'Blackberry']
  const mobiles = {
    name: 'Samsung S22 plus ',
    price: '79,999',
    variants: {
      ram: '8 GB',
      storage: '256 GB',
  
    },
    dateOfPur: d,
    fingerPrint: false,

  }

  res.render('index', {title: 'Express' });
});


router.post('/submit', function (req, res) {
  console.log(req.body)
  MongoClient.connect('mongodb://localhost:27017', function (err, client) {
    if (err) {
      console.log('Error')
    } else {
      console.log('Connected')
    }
  })
  res.send('Data Submitted');
})

module.exports = router;

My app.js file is..

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var aboutRouter = require('./routes/about');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/about', aboutRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

Can anyone solve my problem..please

I have used several approach uninstalling and reinstalling agian and agian different versions of mongo..but it doesn't work..will you please solve my problem..I'm using linux virtual machine in chromebook..I know it has lot of limitations but I had no other option

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Rifan
  • 1
  • 1
  • It means your MongoDB server is either not running, or not accepting connections. There's not much else to say without any information about your MongoDB installation, setup, startup, etc. The only part of the code shown that's relevant is the MongoDB connection. – Dave Newton May 16 '23 at 15:22
  • Tangential, but generally you'd also specify the database in the connection. – Dave Newton May 16 '23 at 15:24
  • do you have a MongoDB server? what is its address? – user253751 May 16 '23 at 17:51

0 Answers0