简体   繁体   中英

Can't connect to MongoClient

I am totally new to MongoDB and node.js and facing this issue.

Do you have any ideas what causes this error?

MongoServerSelectionError: connection <monitor> to 18.158.79.102:27017 closed
   at Timeout._onTimeout (C:\dev\Angular assigments\node-server-ivan\node_modules\mongodb\lib\core\sdam\topology.js:438:30)
   at listOnTimeout (internal/timers.js:554:17)
   at processTimers (internal/timers.js:497:7) {
 reason: TopologyDescription {
   type: 'ReplicaSetNoPrimary',
   setName: 'atlas-i749eb-shard-0',
   maxSetVersion: null,
   maxElectionId: null,
   servers: Map(3) {
     'cluster0-shard-00-00.fivve.mongodb.net:27017' => [ServerDescription],
     'cluster0-shard-00-02.fivve.mongodb.net:27017' => [ServerDescription],
     'cluster0-shard-00-01.fivve.mongodb.net:27017' => [ServerDescription]
   },
   stale: false,
   compatible: true,
   compatibilityError: null,
   logicalSessionTimeoutMinutes: 30,
   heartbeatFrequencyMS: 10000,
   localThresholdMS: 15,
   commonWireVersion: 9
 }
} 

I checked security settings, added outbound rule, reinstall MongoDB but it still doesn't work.

Please find my code below

const bodyParser = require('body-parser')
const MongoClient = require('mongodb').MongoClient
let connectionString = `mongodb+srv://Ivan:<navaro>@cluster0.fivve.mongodb.net/<first-data-base-users>?retryWrites=true&w=majority`

const app = express();
app.use(bodyParser.urlencoded({ extended: true }))
app.listen(3000, () => {
    console.log('listening on 3000')
}) 
 

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index.html') 
})

app.post('/quotes', (req, res) => {
    console.log(req.body)
    console.log('Helooooooooooooooo')
})
//

MongoClient.connect(connectionString, { useUnifiedTopology: true })
    .then(client => {
        console.log('Connected to Database')
        const db = client.db('first-data-base-users')
    })
    .catch(error => console.error(error))

Thank you in advance!

The error says "ReplicaSetNoPrimary". It appears you have set up a cluster with 3 shards

cluster0-shard-00-00.fivve.mongodb.net:27017
cluster0-shard-00-02.fivve.mongodb.net:27017
cluster0-shard-00-01.fivve.mongodb.net:27017

but you have not designated a primary shard in your replica set.

Read more about the primary shard in the MongoDB documentation .

Check this StackOverflow answer for details on how to reconfigure your cluster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM