I'm trying to fetch IDs from one call with mongoose. Afterwards, each of these IDs is used to make another call that returns multiple objects. I am trying to fetch all of these objects.
My current attempt looks something like this:
  var members;
  var memberTimes = [];
  // Use the Group model to find a specific group
  Group.find({
    members: {
      $elemMatch: {
        $eq: req.user._id
      }
    },
    _id: req.params.group_id
  }, function(err, group) {
    if (err) {
      res.send(err);
    } else if (!group) {
      //res.send(new Error("User not in group or it does not exist"));
    }
    members = group[0].members;
    for (var member of members) {
      // Use the Time model to find a specific time
      Time.find({
        user_id: member
      }, function(err, times) {
        if (err) {
          res.send(err);
        }
        for (var time of times) {
          memberTimes.push(time);
        }
      });
    }
    //on completion of all above code, execute res.json(memberTimes);
 });
This, however, does not work because I am not waiting for all the callbacks from the Time#find. I have look at using promises but I am unsure as to how exactly make it work.
Does anyone know how this could be made to work?
Thank you, Daniel
 
     
    