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
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.