在此處輸入影像描述
當我在郵遞員中運行任何 api 呼叫時,我無法連接我的資料庫并遇到此問題,我嘗試了很多東西但無法解決。
uj5u.com熱心網友回復:
這可能是因為
- MongoDB 服務未啟動。
啟動 MongoDB:
sudo systemctl start mongod驗證 MongoDB 是否已成功啟動:
sudo systemctl status mongod
- 該埠已經在提供一些服務。
查找 pid:
lsof -i:27017殺死pid:
kill -9 pid
- 您的連接字串不正確
mongoose.connect('mongodb://localhost:27017/myapp');
- 您的本地主機未配置為使用 mongoDB
uj5u.com熱心網友回復:
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);
})
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/405573.html
標籤:
上一篇:為什么在用這個設定后我會無法為客戶端設定標題......?
下一篇:Node.js應用程式錯誤:MongooseError:`openUri()`的`uri`引數必須是字串,得到“未定義”
