skip to Main Content

I have used MongoDb and Node Js before, unfortunately after reinstalling the two which is after formatting the laptop, connecting the database to Node Js seemingly is not working.
Below is my code

codeimage for the code

1

Output

2

2

Answers


  1. Chosen as BEST ANSWER

    With regards to MongoDB update, instead of using 'localhost' one is required to use '127.0.0.1'.

    Example instead of: mongodb://localhost:27017/Company, one is supposed to write it as; mongodb://127.0.0.1:27017/Company


  2. I was also facing the same problem but now I’m using following code and is working for me

    server.js

    const express = require('express');
    const app = express();
    const mongoose = require('mongoose');
    const app = require('./app');
    const DB = 'mongodb://localhost:27017/Tour-project'
    
    mongoose.connect(DB, (err) => {
       if (err) {
          console.log(err.name, err.message); // display the cause of error for database conenction
          console.error('Error during mongoDB connection');
          console.log('UNHANDLED REJECTION!! 🤦‍♂️🤦‍♂️ ');
       } else {
          // console.log(mongoose.connections);
          console.log('MongoDB connected successfully');
       }
    });
    const port = 3000;
    app.listen(port, 'localhost', () => {
       console.log(`Server is running using port number: ${port}`);
       console.log(`app listening on endpoint ${port}`);
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search