use 127.0.0.1 instead of localhost
xxxxxxxxxx
mongoose.connect("mongodb://127.0.0.1:27017/test")
// type your database name instead of 'test'
xxxxxxxxxx
const mongoose = require("mongoose");
const databaseConnection = () => {
const dbUri = process.env.DB_URI || "mongodb://localhost:27017/codershouse";
mongoose
.connect(dbUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then((data) => {
console.log(
`MongoDb Database Connected to the Server : ${data.connection.host}`
);
})
.catch((err) => {
console.log(`Some Database Connection Error Occured :- ${err}`);
});
};
module.exports = databaseConnection;
xxxxxxxxxx
// local conecation
const mongoose = require("mongoose");
mongoose
.connect("mongodb://localhost:27017/testdb")
.then(() => console.log("Connected to MongoDB..."))
.catch((err) => console.error("Could not connect to MongoDB...", err));