PgB
PgB

Reputation: 93

sails.js find queries with async.js each, parallel calls - each returning early

sails v0.11.0 (http://sailsjs.org/)

I have tried unsuccessfully to use .exec callback, promises (http://sailsjs.org/documentation/reference/waterline-orm/queries) and now async.js (https://github.com/caolan/async) to control the asynchronous flow revolving around looping over a find query. The async.each log output does not have the parallel work in it (although parallel does populate).

So if your solution works using .exec callback, promises or async.js - I will gladly take it!

I found this link gave some helpful examples of async.js (http://www.sebastianseilund.com/nodejs-async-in-practice)

Thank you for your time and assistance.

Below is my code using async:

/**
 * @module      :: Service
 * @type {{findProfile: Function}}
 */

require('async');

module.exports = {
  getProfile: function (userId, callback) {
    var json = {};
    json.notFound = false;
    json.locations = {};
    json.sports = {};

    User.findOne({id: userId}).exec(function (err, user) {
      if (err) {
        json.notFound = true;
        json.err = err;
      }

      if (!err) {
        json.user = user;

        UserSport.find({user_id: user.id}).exec(function (err, userSports) {
          if (err) {
            sails.log.info("userSports error: " + userSports);
          }

          async.each(userSports, function (userSport, callback) {
            LocationSport.findOne({id:userSport.locationsport_id}).exec(function (err, locationSport) {
              if (locationSport instanceof Error) {
                sails.log.info(locationSport);
              }

              async.parallel(
                [
                  function (callback) {
                    Location.findOne({id:locationSport.location_id}).exec(function (err, location) {
                      if (location instanceof Error) {
                        sails.log.info(location);
                      }
                      callback(null, location);
                    });
                  },
                  function (callback) {
                    Sport.findOne({id:locationSport.sport_id}).exec(function (err, sport) {
                      if (sport instanceof Error) {
                        sails.log.info(sport);
                      }
                      callback(null, sport);
                    });
                  }
                ],
                function (err, results) {
                  if (!(results[0].id in json.locations)) {
                    json.locations[results[0].id] = results[0];
                  }

                  if (!(results[1].id in json.sports)) {
                    json.sports[results[1].id] = results[1];
                  }
                }
              ); // async.parallel

            }); // locationSport
            callback();
          }, function (err) {
            sails.log.info('each');
            sails.log.info(json);
          }); // async.each

        }); // UserSport
      }
    }); // User
  }

}

Upvotes: 1

Views: 2926

Answers (1)

arnaud del.
arnaud del.

Reputation: 912

The structure of your code is the following :

      async.each(userSports, function (userSport, callback) {

        // Whatever happen here, it runs asyncly

        callback();
      }, function (err) {
        sails.log.info('each');
        sails.log.info(json);
      }); // async.each

You are calling the callback method, but the processing on your data is not yet done (it is running async-ly). As a result, sails.log.info is called immediatly.

You should modify your code so the callback is called once the process is done. i.e in the result of your async.parallel :

      async.each(userSports, function (userSport, outer_callback) {
        LocationSport.findOne({id:userSport.locationsport_id}).exec(function (err, locationSport) {
          //...
          async.parallel(
            [
              function (callback) {
                // ...
              },
              function (callback) {
                // ...
              }
            ],
            function (err, results) {
              // ...

              outer_callback();

            }
          ); // async.parallel

        }); // locationSport

      }, function (err) {
        sails.log.info('each');
        sails.log.info(json);
      }); // async.each

Upvotes: 2

Related Questions