NodeJs MongooseError: Operation 'x.find()` buffering timed out after 10000ms
Dung Do Tien
Jan 30 2022
100
Hello Guys, I created a small project with Nodejs and MongoDB (Mongoose) as below:
dbhelper.js
const mongoose = require('mongoose');
require('dotenv').config();
module.exports = async () => {
var path ="mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass&directConnection=true&ssl=false";
await mongoose.connect(path, {
keepAlive: true,
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
.then(x => {
console.log(
'Connected to Mongo!'
);
})
.catch(err => {
console.error('Error connecting to mongo', err);
});
return mongoose;
};
index.js
const mongo = require('../dbhelper');
module.exports = async (arg1, arg2, arg3) => {
await mongo().then(mongoose => {
try{
console.log('Connected to mongo!!');
command.execute(client, message, args);
}
finally{
mongoose.connection.close();
}
});
};
But when I tried to execute the function that uses a find()
operation on the model it threw the following error: MongooseError: Operation 'x.find()` buffering timed out after 10000ms.
Anyone can explain for me and how to resolve it?
Have 1 answer(s) found.
-
D0
Dheeraj Yadav Jan 30 2022
Yeah, I have same the problem and the reason is related to opening the connection to MongoDB, you need to use async/await and it will be solved for you:
In your
index.js file
, you can add moreasync/await
in lines 4 and 7 as below:const mongo = require('../dbhelper'); module.exports = async (arg1, arg2, arg3) => { await mongo().then(async mongoose => { // add more async here try{ console.log('Connected to mongo!!'); await command.execute(client, message, args); // add more await here } finally{ mongoose.connection.close(); } }); };
I hope it also worked for you.
* Type maximum 2000 characters.
* All comments have to wait approved before display.
* Please polite comment and respect questions and answers of others.