将MongoDB Atlas集群连接到应用程序时出现此错误,由于将所有IP地址也列入了白名单,因此我不确定如何解决此问题。代码和错误如下所示:
const express = require("express");
const cors = require('cors');
const mongoose = require('mongoose');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 5000;
app.use(cors());
app.use(express.json());`
mongoose.connect('mongodb+srv://surpol:mypassword123@cluster0.kiwls.mongodb.net/mydb123?retryWrites=true&w=majority', {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
})
.then(() => console.log('DB Connected!'))
.catch(err => {
console.log(err);
});
MongooseServerSelectionError: connection <monitor> to 54.156.90.92:27017 timed out
at NativeConnection.Connection.openUri (/Users/suryapolina/Documents/GitHub/Phaseify/backend/node_modules/mongoose/lib/connection.js:828:32)
at Mongoose.connect (/Users/suryapolina/Documents/GitHub/Phaseify/backend/node_modules/mongoose/lib/index.js:335:15)
at Object.<anonymous> (/Users/suryapolina/Documents/GitHub/Phaseify/backend/server.js:15:10)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
at internal/main/run_main_module.js:17:11 {
message: 'connection <monitor> to 54.156.90.92:27017 timed out',
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
setName: 'atlas-wpf52v-shard-0',
maxSetVersion: null,
maxElectionId: null,
servers: Map {
'cluster0-shard-00-00.kiwls.mongodb.net:27017' => [ServerDescription],
'cluster0-shard-00-01.kiwls.mongodb.net:27017' => [ServerDescription],
'cluster0-shard-00-02.kiwls.mongodb.net:27017' => [ServerDescription]
},
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: 30,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: 8
}
}