Can anyone fix the error why am i getting this

My JavaScript Code is this (app.js)

const MongoClient = require(‘mongodb’).MongoClient;
const assert = require(‘assert’);

// Connection URL
const url = “mongodb://localhost:27017”;

// Database Name
const dbName = ‘shopDB’;

// Create a new MongoClient
const client = new MongoClient(url);

// Use connect method to connect to the Server
client.connect(function(err) {
assert.equal(null, err);
console.log(“Connected successfully to server”);

const db = client.db(dbName);

client.close();
});


I run mongod and in other tab when i run node app.js then i am getiing this below errorCcn Any one please fix this or help me in fixing this

I am unable to connect to DataBase.
Please Help !!

1 Like

Either mongod is not running or you have it configured in such a way that it does not answer on the localhost loopback interface.

Hi @Abhishek_Jain4,

It looks like it’s trying to connect on an IPv6 interface, but this is failing (ECONNREFUSED ::1::27017). The driver won’t fall back to IPv4 in these cases (see NODE-4678) so a quick fix is to change your connection string to "mongodb://127.0.0.1:27017" and reconfigure your replicaset (replSetReconfig) so the host details also reference the IP address.

For example (from the shell):

c = rs.conf();
c.members[0].host = "127.0.0.1:27017";
rs.reconfig(c);

The above code sample assumes you only have a single replica set node. Make sure you check your config is correct before making any changes :wink:

Still not able to connect. Can you help more. Please!!