我正在嘗試 npm start 我的后端,但我在連接到 mongodb atlas 時遇到錯誤。這是我的 index.js 代碼:
import express from 'express';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import cors from 'cors';
const app = express();
app.use(bodyParser.json({limit: "30mb", extended: true}));
app.use(bodyParser.urlencoded({limit: "30mb", extended: true}));
app.use(cors());
const CONNECTION_URL = "mongodb srv://Danishbukhari:(mypassword)@cluster0.dxzhf.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"
const PORT = process.env.PORT || 5000;
mongoose.connect(CONNECTION_URL, { useNewUrlParser: true})
.then(() => app.listen(PORT, () => console.log(`Server running on port: ${PORT}`)))
.catch((error) => console.log(error.message) )
我的 package.json 就像:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start": "nodemon index.js"
},
"author": "Danish",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.1",
"cors": "^2.8.5",
"express": "^4.17.2",
"mongodb": "^4.2.2",
"mongoose": "^6.1.2",
"nodemon": "^2.0.15"
}
}

我也將我的 IP 地址列入白名單, 請告訴我如何解決這個問題:(
uj5u.com熱心網友回復:
我有這個問題,并更改DNS來8.8.8.8為我作業。
但在此之前,請確保:
- 檢查您的連接。
- 關閉 VPN。
- IP白名單在
mongoDB - 檢查您的憑據
mongoDB
祝你好運 ;-) 如果仍有問題,請查看鏈接
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/389733.html
標籤:节点.js MongoDB 猫鼬 mongodb-图集
下一篇:為什么設定了readPreference=secondaryPreferred然后在主服務器關閉時無法連接到資料庫?
