Reputation: 19
I have a problem when I try to connect my app with my database with Mongoose. Already tried the following solutions that I found on google:
restarting MongoDB service on windows manually open DB with cmd located on the bin file of MongoDB But I can't solve it. Can anyone help me?
here is my index.js
const { MongoClient } = require('mongodb')
const url = 'mongodb://localhost:27017';
const database = 'e-comm'
const client = new MongoClient(url);
async function getData()
{
let result = await client.connect();
let db = result.db(database);
let collection = db.collection('products');
let response = await collection.find({}).toArray();
console.log(response);
}
getData();
And throw error
C:\Users\HP\new node\node_modules\mongodb\lib\sdam\topology.js:292
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\HP\new node\node_modules\mongodb\lib\sdam\topology.js:29
2: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 {
address: 'localhost:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 32573830,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED ::1:27017
at connectionFailureError (C:\Users\HP\new node\node_modules\mongodb\lib\cmap\co
nnect.js:387:20)
at Socket.<anonymous> (C:\Users\HP\new node\node_modules\mongodb\lib\cmap\connec
t.js:310: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:1300: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.12.1
Upvotes: 2
Views: 3343
Reputation: 28316
By default mongodb listens only on the IP4 localhost 127.0.0.1
.
The error message indicates you are trying to connect to the IP6 localhost ::1
This is likely because you are using the hostname localhost
and your local operating system is resolving to the IP6 address ::1
To fix this you need to have mongoose connect to the same address and port that mongod is listening on, so you will need to either:
a) Change the mongod configuration so it listens on ::1
in addition to or instead of 127.0.0.1
. See net.ipv6
b) Change the mongoose configuration to use the IP address instead of the hostname: const url = 'mongodb://127.0.0.1:27017';
Upvotes: 5