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.org/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.
For 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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With