0

I started a new ExpressJS app and I am having trouble getting connected to my localhost MongoDB on Windows OS.I have read through countless forums and tried everything to get mongod and the mongodb-community server started, but nothing has worked. The error I am getting is below:

connection error: MongooseServerSelectionError: connect ECONNREFUSED ::1:27 at NativeConnection.Connection.openUri (C:\Users\User\Desktop\harta2\no at C:\Users\User\Desktop\harta2\node_modules\mongoose\lib\index.js:340: at C:\Users\User\Desktop\harta2\node_modules\mongoose\lib\helpers\promi at new Promise () at promiseOrCallback (C:\Users\User\Desktop\harta2\node_modules\mongoos at Mongoose._promiseOrCallback (C:\Users\User\Desktop\harta2\node_modul at Mongoose.connect (C:\Users\User\Desktop\harta2\node_modules\mongoose at Object. (C:\Users\User\Desktop\harta2\server.js:5:10) at Module._compile (node:internal/modules/cjs/loader:1097:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149 reason: TopologyDescription { type: 'Unknown', servers: Map(1) { 'localhost:27017' => [ServerDescription] }, stale: false, compatible: true, heartbeatFrequencyMS: 10000, localThresholdMS: 15, logicalSessionTimeoutMinutes: undefined } } node:internal/process/promises:265 triggerUncaughtException(err, true /* fromPromise */);

Mongoose connection code:

const mongoose = require('mongoose');
const express = require('express');
const path = require('path');

mongoose.connect('mongodb://127.0.0.1:27017/wageTeam',{
    useNewUrlParser: true,
    useUnifiedTopology: true
});

const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", () =>{
    console.log("Database connected");
});

const app = express();

Used this to start the server:

@echo off
cd "C:\mongodb\bin"
start mongod.exe
timeout 4
start mongo.exe
exit

2 Answers 2

1

first make sure that mongo service is exist and running because from what I see it is not working

like this

if it is not exist reinstall MongoDB service.

1
  • The mongo service exists and i checked if it was running every time i opened the project
    – EdiTheHigh
    Commented Mar 1, 2022 at 8:06
0

I've changed my node version from v18 to v16.15.0 and it works very fine in my system

Not the answer you're looking for? Browse other questions tagged or ask your own question.