Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose .find() method causes requests to hang

I have this route defined, but any requests made to it get's stuck on 'pending' and runs forever.

When I log the code, I see 1 followed by 4, which means the code inside the find method never gets executed

  # Calendar routes
  router.get '/calendars', (req, res) ->
    console.log '1'
    Calendar.find (err, calendars) ->
      console.log "2" + err
      console.log "3" + calendars
      res.send(err) if err
      res.json(calendars)
      return
    console.log '4'
    return

Model

mongoose = require("mongoose")

module.exports = mongoose.model("Calendar",
  name: String
)

Any ideas on why this is?

like image 328
Tarlen Avatar asked Dec 06 '14 11:12

Tarlen


People also ask

What does find do in Mongoose?

The find() function is used to find particular data from the MongoDB database. It takes 3 arguments and they are query (also known as a condition), query projection (used for mentioning which fields to include or exclude from the query), and the last argument is the general query options (like limit, skip, etc).

What does find function return in Mongoose?

What is find() in Mongoose? Mongoose's find() method is a query to retrieve a document or documents that match a particular filter.

Does find return an array Mongoose?

and it will return array? find returns an array always.

Can I use $in in Mongoose?

The simple and easy way is to use the $in operator. The $in operator takes an array as its value. And $in operator, in turn, is assigned to the field according to which the filtration is to be done. Let's use the $in operator.


1 Answers

Until you call mongoose.connect, your mongoose queries will simply be queued up.

Add code like this in your startup code to connect:

mongoose.connect('mongodb://localhost/test', function(err) {
    if (err) {
        console.error(err);
    } else {
        console.log('Connected');
    }    
});

In the connection string, replace test with the name of your database.

like image 124
JohnnyHK Avatar answered Sep 28 '22 09:09

JohnnyHK