0

I am building an app using the MERN stack. I am trying to connect MongoDB via the localhost:27017 method. When I run node app.js my application opens and I can access it by going to localhost:4000 in my browser but when it tries to connect to the database that's when it gets hung up and throws this error:

    Oh no Mongo Connection error!!
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at Connection.openUri (C:\Users\steph\OneDrive\Documents\GitHub\CRM-Tool\node_modules\mongoose\lib\connection.js:819:32)
    at C:\Users\steph\OneDrive\Documents\GitHub\CRM-Tool\node_modules\mongoose\lib\index.js:378:10
    at C:\Users\steph\OneDrive\Documents\GitHub\CRM-Tool\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (C:\Users\steph\OneDrive\Documents\GitHub\CRM-Tool\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (C:\Users\steph\OneDrive\Documents\GitHub\CRM-Tool\node_modules\mongoose\lib\index.js:1223:10)
    at Mongoose.connect (C:\Users\steph\OneDrive\Documents\GitHub\CRM-Tool\node_modules\mongoose\lib\index.js:377:20)
    at Object.<anonymous> (C:\Users\steph\OneDrive\Documents\GitHub\CRM-Tool\app.js:16:10)
    at Module._compile (node:internal/modules/cjs/loader:1112:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1166:10) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) { 'localhost:27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    logicalSessionTimeoutMinutes: undefined
  },
  code: undefined
}

I have installed MongoDB onto my machine and I have the mongod.exe binary. I also made the folders C:\data\db because that is the default location where mongod.exe stores its data i guess so it has to exist. I also have MongoDB Compass installed and I am able to connect over port 27017 and have created the /crm database that I have specified in my code. I feel like the cmd connection method mentioned above is actually redundant when I already have the ability to connect over Compass. I just went down that path to see if it might fix the problem but it hasn't. This is my app.js code:

    require('dotenv').config()
const express = require('express');
const app = express();
const path = require('path');
const ejs = require('ejs');
const ejsMate = require('ejs-mate');
const mongoose = require('mongoose');
const methodOverride = require('method-override'); //to be able to use app.put

// require routes USER, PRODUCT, CUSTOMER, SUPPLIER
const productRoutes = require('./routes/products');
const supplierRoutes = require('./routes/supplier');
const customerRoutes = require('./routes/customer')

// mongoose.connect(process.env.DATABASE_URL, { useNewUrlParser: true })
mongoose.connect('mongodb://localhost:27017/crm', { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => {
    console.log("Mongo connection open!!!");
  })
  .catch((err) => {
    console.log("Oh no Mongo Connection error!!");
    console.log(err);
  })


// Static Files
app.use(express.static(path.join(__dirname, 'public')));


// ejs allows us to use javascript codes within html files
app.set('view engine', 'ejs');
app.engine('ejs', ejsMate);

// to setup the path
app.set('views', path.join(__dirname, 'views'));

// Parsing Middleware
app.use(express.urlencoded({extended:true}))
app.use(methodOverride('_method'));

// To use the exported routes
app.use('/dashboard/products', productRoutes);
app.use('/dashboard/suppliers', supplierRoutes);
app.use('/dashboard/customers', customerRoutes);

// show routes for REGULAR PAGES
app.get('/', (req, res) => {
  res.render('main');
});

app.get('/login', (req, res) => {
  res.render('login');
})

// this is just for designing purposes,
// must be deleted once deployed because dashboard must depend on who was logged in
app.get('/dashboard', (req, res) => {
  res.render('dashboard');
})

app.post('/dashboard', (req, res) => {
  console.log('you are logged in')
  res.render('dashboard');
})

// CUSTOMER'S SECTION
app.get('/dashboard/customers', (req, res) => {
  res.render('pages/customers/show');
})


app.listen(4000, () => {
  console.log('Serving port 4000')
});

As I said, the app shows up in my browser over port 4000 but the database never gets up and running. It just times out and throws the error message above. I just want to be able to get my app connected to the localhost:27017 database so I can start sending data to the server. Thanks.

  • Your code is connecting to MongoDB using IPv6 (::1:27017). Is MongoDB set up for IPv6? If not, change **localhost** to **127.0.0.1**. – John Hanley Jul 30 '22 at 00:51

1 Answers1

0

It is possible that your mongodB isn't running.

A similar question was asked here, and it has been answered.

MongoError: connect ECONNREFUSED 127.0.0.1:27017

Lord Bee
  • 132
  • 6
  • Thanks, that link was really helpful. I figured out that changing ```mongodb://localhost1:27017``` to either ```mongodb://127.0.0.1:27017``` or ```mongodb://0.0.0.0:27017``` fixes the problem. – Stephen Hammonds Jul 31 '22 at 15:13