8

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?

Tarlen
  • 3,657
  • 8
  • 30
  • 54

1 Answers1

14

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.

tjbp
  • 3,427
  • 3
  • 24
  • 35
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471