Home > other >  MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 at Connection.openUri
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 at Connection.openUri

Time:01-03

I am getting error in my code that [nodemon] app crashed - waiting for file changes before starting... Mongodb is running in my pc.

app.js

const express = require("express");
const app = express();

app.use(express.json());

const product = require("./routes/ProductRoutes");

app.use("/api/v2", product);

module.exports = app;

server.js

const app = require("./app");
const dotenv = require("dotenv");
const connectDatabase = require("./db/Database");

dotenv.config({
  path: "backend/config/.env",
});

connectDatabase();

const server = app.listen(process.env.PORT, () => {
  console.log(`Server is running on https://localhost:${process.env.PORT}`);
});

database.js

const mongoose = require("mongoose");

const connectDatabase = () => {
  mongoose
    .connect(process.env.DB_URL, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    })
    .then((data) => {
      console.log(`mongodb is connected with server: ${data.connection.host}`);
    });
};

module.exports = connectDatabase;

.env

  PORT = 4000 
  DB_URL = "mongodb://localhost:27017/ecommerce"

Mongodb is running in my pc.

CodePudding user response:

Try:

DB_URL = "mongodb://127.0.0.1:27017/ecommerce"

instead of

DB_URL = "mongodb://localhost:27017/ecommerce"

source:

https://www.mongodb.com/community/forums/t/mongooseserverselectionerror-connect-econnrefused-127-0-0-1-27017/123421/2

If no works provide mongoose, mongo and node version.

CodePudding user response:

try using this instead.

mongoose.connect('mongodb://127.0.0.1/ecommerce')
  • Related