【问题标题】:Database connection problem and api calls数据库连接问题和api调用
【发布时间】:2022-01-05 04:54:39
【问题描述】:

enter image description here

当我在邮递员中运行任何 api 调用时,我无法连接我的数据库并遇到此问题,我尝试了很多东西但无法解决。

【问题讨论】:

  • 你能分享你的 server.js 文件吗?

标签: javascript mongodb mongoose mongoose-schema


【解决方案1】:

这可能是因为

  1. MongoDB 服务未启动。

启动 MongoDB:sudo systemctl start mongod

验证 MongoDB 是否已成功启动:sudo systemctl status mongod

  1. 端口已经在提供一些服务。

查找 pid:lsof -i:27017

杀死pid:kill -9 pid

  1. 您的连接字符串不正确

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

  1. 您的 localhost 未配置为使用 mongoDB

【讨论】:

    【解决方案2】:

    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);
        })
    })

    【讨论】:

      猜你喜欢
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      • 2016-12-08
      相关资源
      最近更新 更多