I am getting this Mongoose Error: “Operation users.insertOne()
buffering timed out after 10000ms”, when I am trying to Create new user. I have already tried changing DNS to 8.8.8.8
Kindly suggest any solution. I am stuck due to this.
Index.js
const express = require(‘express’)
const app = express()
const port = 5000
const mongoDB = require(“./db”)
app.use((req, res, next) => {
res.setHeader(“Access-Control-Allow-Origin”, “http://localhost:3000”);
res.header(
“Access-Control-Allow-Headers”,
“Origin, X-Requested-With, Content-Type, Accept”
);
next();
})
app.use(express.json())
app.use(‘/api’, require(“./Routes/CreateUser”))
app.get(‘/’, (req, res) => {
res.send(‘Hello World!’)
})
app.listen(port, () => {
console.log(Example app listening on port ${port}
)
})
User.js
const mongoose = require(‘mongoose’)
const { Schema } = mongoose;
const UserSchema = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
location: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
}
});
module.exports = mongoose.model(‘user’,UserSchema)
db.js
const mongoose = require(‘mongoose’);
const mongoURL = ‘mongodb+srv://Gofood:cluster0.1mqwzx1.mongodb.net/Gofoodmern?retryWrites=true&w=majority&ssl=true’
const mongooseOptions = {
connectTimeoutMS: 50000, // Increase the connection timeout to 30 seconds
socketTimeoutMS: 50000, // Increase the socket timeout to 45 second
};
const mongoDB = async () => {
await mongoose.connect({mongoURL, mongooseOptions, useNewUrlParser: true, useUnifiedTopology: true}, async (err, result) => {
if (err) console.log("---", err)
else {
console.log("connected");
const fetched_data = await mongoose.connection.db.collection("food_menu");
fetched_data.find({}).toArray(function(err, data){
if(err) console.log(err);
else console.log();
})
}
});
}
module.exports = mongoDB;
CreateUser.js
const express = require(‘express’)
const router = express.Router()
const User = require(‘…/models/User’)
const { body, validationResult } = require(‘express-validator’);
router.post(“/createuser”,
[
body(‘name’).isLength({ min: 3 }),
body(‘email’).isEmail(),
body(‘password’, ‘Incorrect Password’).isLength({ min: 5 })
],
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
try {
await User.create({
name: req.body.name,
password: req.body.password,
email: req.body.email,
location: req.body.location
})
res.json({ success: true }) //.then(res.json({ success: true }));
}
catch (error) {
console.log(error)
res.status(500).json({ success: false, error: "Internal server error" });
//res.json({ success: false });
}
}
)
module.exports = router;
Terminal
MongooseError: Operation users.insertOne()
buffering timed out after 10000ms
at Timeout. (E:\Web Devp Projects\HungryBites\mernapp\backend\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:175:23)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7)
Response on Browser Console and thunderclient(vs code)
{
“success”: false,
“error”: “Internal server error”
}