Vince V.
Vince V.

Reputation: 3143

Mongoose in function doesn't return anything

I'm trying to query my database but for some reason I never get results when I know there are 3 things into my database. I've made this function:

function toJson()
{

  var test = [];

  async.series({
    rooms : function() { return Room.find(); }
  }
  , function(err, results) {

    test = results.rooms;

  });

  return test;

}

How does this come ? I'm guessing it has something to do that mongoose his method (search) is async..

Thanks in advance.

Upvotes: 0

Views: 182

Answers (1)

Asherah
Asherah

Reputation: 19347

toJson is returning immediately, but the return test; happens immediately. You need to make toJson take a callback instead—you don't even need to use async.series here:

function toJson(callback) {
    Room.find(function(err, results) {
        callback(results);
    });
}

This will kind of do what you want -- but you shouldn't ignore err like you're proposing.

Upvotes: 4

Related Questions