I have a collection on MongoDB from which I'm trying to query all the elements using find()
:
const mongoose = require('mongoose');
const Featured = mongoose.model('featured');
module.exports = app => {
app.get('/api/featured', async (req, res) => {
console.log("featured route");
const featured = await Featured.find();
console.log(featured);
res.send(featured);
})
}
Here's Featured.js:
const mongoose = require('mongoose');
const { Schema } = mongoose;
const featuredSchema = new Schema({});
mongoose.model('featured', featuredSchema);
However, I'm getting the error upon making the request:
(node:75568) UnhandledPromiseRejectionWarning: MongooseError: Operation `featureds.find()` buffering timed out after 10000ms
at Timeout.<anonymous> (/Users/prikshetsharma/Desktop/humboiserver/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:184:20)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)
(node:75568) 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 .html#cli_unhandled_rejections_mode). (rejection id: 2)
How to fix this error and get all the collection items to return with find()
? Strangely, the error shows featureds.find()
whereas I've never used featureds word in my code anywhere.
I have a collection on MongoDB from which I'm trying to query all the elements using find()
:
const mongoose = require('mongoose');
const Featured = mongoose.model('featured');
module.exports = app => {
app.get('/api/featured', async (req, res) => {
console.log("featured route");
const featured = await Featured.find();
console.log(featured);
res.send(featured);
})
}
Here's Featured.js:
const mongoose = require('mongoose');
const { Schema } = mongoose;
const featuredSchema = new Schema({});
mongoose.model('featured', featuredSchema);
However, I'm getting the error upon making the request:
(node:75568) UnhandledPromiseRejectionWarning: MongooseError: Operation `featureds.find()` buffering timed out after 10000ms
at Timeout.<anonymous> (/Users/prikshetsharma/Desktop/humboiserver/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:184:20)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)
(node:75568) 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/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
How to fix this error and get all the collection items to return with find()
? Strangely, the error shows featureds.find()
whereas I've never used featureds word in my code anywhere.
5 Answers
Reset to default 3For anyone else who might stumble upon this: My issue had to do with a faulty connection, and I managed to fix it by using mongoose.connect
instead of mongoose.createConnection
.
Please note the Mongoose documentation saying:
Mongoose will not throw any errors by default if you use a model without connecting.
...which just results in a buffering timeout instead.
if you are on localhost, using
mongoose.connect('mongodb://localhost:27017/myapp');
try using 127.0.0.1 instead of localhost
mongoose.connect('mongodb://127.0.0.1:27017/myapp');
Quick Fixes:
- Export model in
Featured.js
:
const mongoose = require('mongoose');
const { Schema } = mongoose;
const featuredSchema = new Schema({}, { collection: "featured" });
module.exports = mongoose.model('featured', featuredSchema);
UnhandledPromiseRejectionWarning: Unhandled promise rejection,
- You need to wrap your service code in try catch block,
const mongoose = require('mongoose');
// correct this path to your original path of Featured.js
const Featured = require('./Featured.js');
app.get('/api/featured', async (req, res) => {
try {
console.log("featured route");
const featured = await Featured.find();
console.log(featured);
res.send(featured);
}
catch(e) {
console.log('Catch an error: ', e)
}
});
featureds.find()
buffering timed out after 10000ms,
- there would be many possibilities,
- Remove the mongoose module from node_module and also from *.json files, reinstall mongoose module and try again.
- Check if you have connected with database or not, after that check if you have the correct network access to your database cluster.
If anyone is using Mongo db Atlas then they need to whitelist their IP address to authorize the access.
Steps to authorize the access.
- Get your systems IP address
For Mac User : Hit this mand on terminal . curl ifconfig.me
For Windows user : Hit this mand on mand Prompts . ipconfig /all
You can find your IP Address by web also. eg. https://whatismyipaddress./
Once you have your networks IP address:
Go to Mongo DB Atlas -> Network Access -> IP Access List - Add your IP address. You can share access to specific IP address or you can keep open access for all as well.
I had everything working including having my IP Address whitelist set to 0.0.0.0/0
and the connection being set up just fine. For some reason when I would try to run my server.mjs
module (where I create my server and establish my connection to mongoDB via mongoose), it would start running one of my other files using find()
before running server.mjs
.
A very hacky solution but I just shifted the function using find()
to the end of the file so that server.mjs
runs and thus connection is established first.
This should not have happened and I am still confused on why server.mjs
didn't run first but this solution worked for me.
Hope it helps!!