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

Viewed 17673

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.

4 Answers

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 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,
  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.

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 command on terminal . curl ifconfig.me

For Windows user : Hit this command on command Prompts . ipconfig /all

You can find your IP Address by web also. eg. https://whatismyipaddress.com/

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.

enter image description here

Related