I have been trying to connect my mongoose database locally to 27017 but it is refusing to connect.
I thought it might be an error in my code but all my other apps that I had previously built, that were fully functional and working before, have the same error when I try to run them.
I tried using the mongodb compass and running the mongod command in cmdm but nothing seems to work. What could be the reason for it?
I tried reinstalling mongodb but it didn't work as well.
This is the error output:
\Desktop\rest-api\node_modules\mongoose\lib\connection.js:792
err = new ServerSelectionError();
^
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at _handleConnectionErrors (C:\Users\siddh\OneDrive\Desktop\rest-api\node_modules\mongoose\lib\connection.js:792:11)
at NativeConnection.openUri (C:\Users\siddh\OneDrive\Desktop\rest-api\node_modules\mongoose\lib\connection.js:767:11)
at runNextTicks (node:internal/process/task_queues:60:5)
at listOnTimeout (node:internal/timers:537:9)
at process.processTimers (node:internal/timers:511: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: 557344,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED ::1:27017
at connectionFailureError (C:\Users\siddh\OneDrive\Desktop\rest-api\node_modules\mongodb\lib\cmap\connect.js:370:20)
at Socket.<anonymous> (C:\Users\siddh\OneDrive\Desktop\rest-api\node_modules\mongodb\lib\cmap\connect.js:293:22)
at Object.onceWrapper (node:events:627:26)
at Socket.emit (node:events:512: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: -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
}
This is my code:
import express from 'express';
import { APP_PORT } from './config/index.js'; //We do not need to
const app = express();
import mongoose from 'mongoose';
mongoose.connect('mongodb://localhost:27017/rest-api', {useNewUrlParser: true, useUnifiedTopology: true}).then(()=>{
console.log('Database connected');
});
app.listen(APP_PORT,()=>{console.log(`Server running on port ${APP_PORT}`)})