简体   繁体   中英

Getting Mongo Network Error while connecting to mongo atlas uri

I am using mongo Atlas uri and getting mongo network error . Sometimes it works but sometimes it throws error.

Error: connect ETIMEDOUT 13.234.241.108:27017

Server started on port 5000
Error: connect ETIMEDOUT 13.234.241.108:27017
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16) {
  name: 'MongoNetworkError'
}
something wrong with DB

mongoDb Uri

mongoose
  .connect(
    `mongodb+srv://pratham:<password>@cluster0.gxwve.mongodb.net/twitter-clone?retryWrites=true&w=majority`,
    {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useCreateIndex: true,
      useFindAndModify: false,
      keepAlive: true,
    }
  )
  .then(() => {
    console.log("DB Connected");
  })
  .catch((err) => {
    console.log(err);
    console.log("something wrong with DB");
  });

Make sure the following two things

  1. Make sure MongoDB is running on the specified machine
  2. Make sure the bindIP in config is modified to accept connections from other IPs

I think it may be something wrong with the MongoDB server. This error occurs due to a low internet connection. Just wait a bit and the program worked as usual. Also, use Async and wait when every your make request with MongoDB server

const connectDB = async () => {
    const conn = await mongoose.connect(`mongodb+srv://pratham:<password>
        @cluster0.gxwve.mongodb.net/twitter-clone?retryWrites=true&w=majority`, {
        useNewUrlParser: true,
        useCreateIndex: true,
        useFindAndModify: false,
        useUnifiedTopology: true
    }).then(() => {
        console.log("DB Connected");
    }).catch((err) => {
        console.log(err);
        console.log("something wrong with DB");
    });
};

// use as a function        
connectDB();

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