Home > database >  Database connection problem and api calls
Database connection problem and api calls

Time:01-06

enter image description here

I am not able to connect my database and getting this problem when i run any api call in postman ,I have tried so many things but not able to get it out .

CodePudding user response:

This is probably because

  1. The MongoDB service isn't started.

Start MongoDB: sudo systemctl start mongod

Verify that MongoDB has started successfully: sudo systemctl status mongod

  1. The port is already serving some service.

Find pid: lsof -i:27017

Kill pid: kill -9 pid

  1. Your connection string is incorrect

mongoose.connect('mongodb://localhost:27017/myapp');

  1. Your localhost is not configured to use mongoDB

CodePudding user response:

const app=require("./app");
const dotenv=require("dotenv");
const connectDatabase=require("./config/database");
PORT=4000;

//Handling uncaught Exception(YouTube Wala Error)
process.on("uncaughtException",(err)=>{
    console.log(`Error:${err.message}`);
    console.log(`Shuting Down the server due to uncaught Exception`);
    process.exit(1);
})




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


//Connecting to database
connectDatabase();

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

   



//Unhandled Promise Rejection-> in case when we use mongo(using)
process.on("unhandledRejection",err=>{
    console.log(`Error: ${err.message}`);
    console.log(`Shuting Down the server due to Unhandled Promise Rejection`);

    server.close(() =>{ 
   process.exit(1);
    })
})

  •  Tags:  
  • Related