1

Database code:

const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient;

const mongoConnect = callback => {
MongoClient.connect('mongodb+srv://shubhamt10:shubham123@cluster0-g2s9w.mongodb.net/test?retryWrites=true',
{ useNewUrlParser: true})
.then(client => {
console.log('Connected!');
callback(client);
})
.catch(err => {
console.log(err);
});
}

module.exports = mongoConnect;

app.js code

const mongoConnect = require('./util/database');

mongoConnect(client => {
console.log(client);
app.listen(3000);
});

I am getting this error : { MongoNetworkError: connection 5 to cluster0-shard-00-02-g2s9w.mongodb.net:27017 closed at TLSSocket. (E:\Node JS\Final Project\node_modules\mongodb-core\lib\connection\connection.js:276:9) at Object.onceWrapper (events.js:285:13) at TLSSocket.emit (events.js:202:15) at _handle.close (net.js:611:12) at TCP.done (_tls_wrap.js:390:7) name: 'MongoNetworkError', errorLabels: [ 'TransientTransactionError' ], [Symbol(mongoErrorContextSymbol)]: {} }

  • 1
    Check https://stackoverflow.com/questions/52153538/what-is-a-transienttransactionerror-in-mongoose-or-mongodb on my case I restarted the MongoDB Service on Windows and it was the solution – fedeteka Feb 25 '19 at 13:10

1 Answers1

0

Please check the network access Of your MongoDB atlas account

enter image description here

click on the Allow access from anywhere button after that click on confirm button to change the IP address 0.0.0.0/0 now you can use your connection string on any system

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103