I have this code in Javascript, using Sails and MongoDB, but the execution of the code is not as I am expecting. The first part of the code is executed at first but the addition to the "foundwaiters" array is executed only at the end (after the logged out case) which is a problem because the array is not updated when the HTML page shows up. How can I fix this ? Thank you.
Tutorial.find(...)
          .populate('videos')
          .exec(function (err, foundTutorials){
            _.each(foundTutorials, function(tutorial){
              _.each(tutorial.videos, function(video){
                User.find().populate('isWaiting').exec(function (err, foundusers){
                  _.each(foundusers, function(user){
                  _.each(user.isWaiting, function(myvideo){
                     if (myvideo.id === video.id ) {
                      foundwaiters.push(user);
                     }
                  });
                });
                });
              });
          });
            // The logged out case
            if (!req.session.userId) {
              return res.view('profile', {
                // This is for the navigation bar
                me: null,
                // This is for profile body
                username: foundUser.username,
                gravatarURL: foundUser.gravatarURL,
                frontEnd: {
                  numOfTutorials: foundTutorials.length,
                  numOfFollowers: foundUser.followers.length,
                  numOfFollowing: foundUser.following.length,
                  /// ###########
                  numOfWaiters:foundwaiters.length
                  // ############
                },
                // This is for the list of tutorials
                tutorials: foundTutorials,
                //   #######
                waiters:foundwaiters
              });
            }
 
     
    