'How to resolve `buffering timed out after 10000ms` error in MongodoDB and Mongoose
I am using MongoDb in my Nodejs App with mongoose as ORM. However I am getting this error everytime I run my code.
(node:28316) UnhandledPromiseRejectionWarning: MongooseError: Operation `auths.findOne()` buffering timed out after 10000ms
at Timeout.<anonymous> (/home/mridu/Projects/unoletter/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:149:23)
at listOnTimeout (internal/timers.js:557:17)
at processTimers (internal/timers.js:500:7)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:28316) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:28316) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Here is my code to connect with MongoDb atlast
try {
mongoose.connect(
process.env.DB_URI,
{
keepAlive: true,
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
},
() => {
console.log(`connected to MongoDB`);
}
);
} catch (error) {
console.log(error);
}
My model
//Dependencies
const mongoose = require("mongoose");
//Auth Model
const auth = {
id: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
secret: {
type: String,
required: true,
},
isVerified: Boolean,
createdAt: {
time: String,
date: String,
},
};
const Auth = mongoose.model("Auth", auth);
//Export
module.exports = { Auth };
And this is how I am saving my data
const auth = new Auth({
id: generateId(),
email: body.email,
secret: generateSecret(),
isVerified: false,
createdAt: {
time: dayjs().format("HH:mm:ss"),
date: dayjs().format("YYYY/MM/DD"),
},
});
try {
await auth.save();
console.log(auth);
} catch (error) {
console.log(error);
}
I have tried some answers from Stackoverflow and other websites like Dev.to. I have tried using async/await, .then & .catch. Still I am facing the same error.
Another project where I am using the 5.xx version of Mongoose is working fine. I am facing this issue with new 6.0.3 version of mongoose.
Solution 1:[1]
Make sure to call the function that uses the connection, an example can be the following. I have the following function:
const start = async () => {
try {
await connectDB(process.env.MONGO_URI);
await Product.deleteMany();
await Product.create(jsonProducts);
// console.log(jsonProducts)
console.log('Conexion exitosa')
process.exit(0)
} catch (e) {
console.log(e)
process.exit(1)
}
}
It turned out that I was declaring the function but not using it, my solution was just to execute the start() function.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tyler2P |
