1

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!

1 Answers1

0

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.

Codebling
  • 10,764
  • 2
  • 38
  • 66
  • Could you show me an example of code that fixes it? I've read stuff that you sent and official documentation also, but I can't figure out where exactly in my code I should to implement it. – Ivan Bozhko Feb 09 '21 at 09:48
  • @IvanBozhko unfortunately it's not in your code that this must be fixed, if I am correct. My understanding is that the cloud cluster is set up incorrectly. The configuration on the MongoDB servers must change. Is anyone else using those same MongoDB servers? You could also try changing `connectionString` and replacing `cluster0.fivve.mongodb.net` with `cluster0-shard-00-00.fivve.mongodb.net`, but I don't think this will work. – Codebling Feb 09 '21 at 18:12
  • 1
    Thank you I'll try to fix it in this way. I use it alone. I'll play around, thank you for your advice. Already fixed connectionString, but still doesn't work. – Ivan Bozhko Feb 09 '21 at 22:34
  • @IvanBozhko, I think your original `connectionString` was correct, and once the server configuration is fixed it should work. Good luck – Codebling Feb 09 '21 at 23:37
  • @IvanBozhko were you able to get it to work? – Codebling Feb 15 '21 at 23:09