Exercise tracker - query & filtering

Exercise tracker - query & filtering
0

UPDATE: I’ve decided to do this a different way,
I’m returning the data first and then manipulating the array.

const getLogs = function (userid, from, to, limit) {
  try {
    if (userid && from) {
      const exerciseLogs = UserModel.findOne(
        {
          "_id": userid,
          log: { // log is the name of the sub-array
            $elemMatch: {
              date: {
                $gte: new Date(from)
              }
            }
          }
        }
      );
      return exerciseLogs; // returns same result as:  else if (userid)
    } else if (userid) {
      exerciseLogs = UserModel.findOne({ "_id": userid });
      return exerciseLogs;
    }
    return exerciseLogs;
  } catch (e) {
    console.log("e:  " + e);
  }
};

The else if works perfectly from this url: /api/exercise/log?userId=f72MONg0H

But the first if statement (if (userid && from) ) returns the same result from this url:
/api/exercise/log?userId=f72MONg0H&from=2018-12-07

I can’t figure out if it’s ignoring $elemMatch

hi Sonnerz
you do not need to use $elemMatch in your query when you specify only one condition
also the last return exerciseLogs is outside the if block, you do not have access to exerciseLogs and i think you can delete it
please show me your UserModel