7

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.

turivishal
  • 34,368
  • 7
  • 36
  • 59
zendevil
  • 899
  • 4
  • 13
  • 26

4 Answers4

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');
Mohammed Saeid
  • 281
  • 2
  • 8
2

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.

o01
  • 5,191
  • 10
  • 44
  • 85
  • mongoose.connect will not allow multiple connection? I will have to use mongoose.createConnection instead. In that case how to solve this? – Anonymous Creator May 30 '21 at 14:14
0

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.
turivishal
  • 34,368
  • 7
  • 36
  • 59
  • I've reinstalled and edited out mongoose from package.json. The database is also connected. I think the problem is that it's showing featureds.find() and not featured.find(), and I don't know why that's the case, because the name of the collection is featured and nowhere I've used the word featureds. – zendevil Dec 23 '20 at 11:55
  • After wrapping in the try catch, I'm also getting the following: nhandledPromiseRejectionWarning: ReferenceError: featured is not defined – zendevil Dec 23 '20 at 11:57
  • i have updated answer, export model from Featured.js and use that using require('Featured.js') – turivishal Dec 23 '20 at 12:06
  • The error is gone, but this returns an empty array and not the elements that are contained in the collection. – zendevil Dec 23 '20 at 12:31
  • When creating a new Featured, the collection is being saved as featureds, and not in featured. – zendevil Dec 23 '20 at 12:40
  • you just need to make sure your collection name is `featured`, or i have added collection name in schema Featured.js file check edited answer – turivishal Dec 23 '20 at 12:40
0

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

Maheshvirus
  • 6,749
  • 2
  • 38
  • 40