0

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.

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.