WebQube
WebQube

Reputation: 8981

javascript express js passing async resuls

I'm new to js. I am using express for node js, and mongoose as a mongo orm.

function direct_tags_search_in_db(tags){
    var final_results = [];
    for (var i=0; i<tags.length; ++i) {
        var tag = tags[i];
        Question.find({tags: tag}).exec(function(err, questions) {
            final_results.push(questions);
            if (i == tags.length -1 ){
              return final_results;
            }
        });
    }
};

I get empty results, because of the asynchronously of the find. But I don't know what the best approach for this.

Appriciate a little help, thanks.

Upvotes: 1

Views: 54

Answers (1)

Kevin Reilly
Kevin Reilly

Reputation: 6252

You will often find that methods such as Question.find().exec that accept a function as an argument are async. It is especially common for methods that perform network requests or file system operations. These are most commonly referred to as a callback. That being the case, if you would like something to occur when the async task(s) complete, you need to also implement a callback.

Also, it is possible that your reference to tag is being changed in a way that is likely undesired. There are a number of solutions, here is a simple one.

function direct_tags_search_in_db(tags, callback){
    var final_results = [];
    // Array.forEach is able to retain the appropriate `tag` reference
    tags.forEach(function(tag){
        Question.find({tags: tag}).exec(function(err, questions) {
            // We should be making sure to handle errors
            if (err) {
              // Return errors to the requester
              callback(err);
            } else {
              final_results.push(questions);
              if (i == tags.length -1 ){
                // All done, return the results
                callback(null, final_results);
              }
            }
        });
    });
};

You will notice that when we implement our own callback, that we follow the same common pattern as the callback for Question.find().exec(function(err, result){}); -- first argument a potential error, second argument the result. That is why when we return the results, we provide null as the first argument callback(null, final_results);

Quick example of calling this function:

direct_tags_search_in_db([1, 2, 3], function(err, results){
  if (err) {
    console.error('Error!');
    console.error(err);
  } else {
    console.log('Final results');
    console.log(results);
  }
});

Another option for solving various async goals is the async module, promises, or otherwise.

Upvotes: 1

Related Questions