'MongooseError: Operation 'featureds.find()` buffering timed out after 10000ms

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.



Solution 1:[1]

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.

Solution 2:[2]

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,
  1. Remove the mongoose module from node_module and also from *.json files, reinstall mongoose module and try again.
  2. Check if you have connected with database or not, after that check if you have the correct network access to your database cluster.

Solution 3:[3]

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');

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 o01
Solution 2
Solution 3 Mohammed Saeid