0

I failed to connected MongoDB with server and the codes are given below, please concern below,

app.js:

const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json());

module.exports = app;

.env:

MONGODB_SERVER = mongodb://localhost:27017/burger-builder
PORT = 3001

server.js:

const dotenv = require('dotenv');
dotenv.config();
const app = require('./app');
const mongoose = require('mongoose');

mongoose.connect(process.env.MONGODB_SERVER, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useCreateIndex: true,

})
     .then(() => console.log('Connect to MongoDB!'))
     .catch(err => console.log('MongoDB Connection Failed!'));

const port = process.env.PORT || 3001;

app.listen(port, () =>{
     console.log(`Listening on port ${port}`);
})

Please concern the attached file for checking the error.

Note: In .env file I connected with the location as burger builder but there is no any file . Is it the main probel to failed the MongoDB with the server?

4
  • 2
    What error do you see? Commented Jun 3, 2021 at 4:54
  • 1
    please log the error -> .catch(err => console.log('MongoDB Connection Failed!', err.stack)); Commented Jun 3, 2021 at 5:01
  • make sure your mongodb is running in background. Commented Jun 3, 2021 at 5:03
  • 1
    console log the process.env.MONGODB_SERVER , verify if its not empty Commented Jun 3, 2021 at 6:45

1 Answer 1

1
  1. Can you console log the error?
  2. Can you make sure mongodb is running?
  3. Can you make sure the uri is correct? port is correct?
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot MongoDB was not running.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.