Exercise Tracker- Log endpoint not passing

Hi,
I’m currently finishing off my API and Microservices certification, but I can’t seem to get one test to pass.
The test I can retrieve a full exercise log of any user by getting /api/exercise/log with a parameter of userId(_id). App will return the user object with added array log and count (total exercise count). isn’t working. I’ve combed over it many times, compared it to the demo output and it is identical. I’m completely bewildered, and could do with some guidance.
Here is my code so far for the /api/exercise/log endpoint:

app.get("/api/exercise/log", async (req, res) => {
  //REQUIRED
  let userid = req.query.userId;
  //OPTIONAL
  let from = req.query.from;
  let to = req.query.to;
  let limit = req.query.limit;
  let query = {};

  //Construct query based on what we are given
  query.userId = userid;

  if (from !== undefined) {
    from = new Date(from);
    query.date = { $gte: from };
  }

  if (to !== undefined) {
    to = new Date(to);
    to.setDate(to.getDate() + 1); // Add 1 day to include date
    query.date = { $lt: to };
  }

  if (from !== undefined && to !== undefined) {
    from = new Date(from);
    to = new Date(to);
    query.date = { $gte: from, $lt: to };
  }

  User.findById(userid, function (err, userinfo) {
    if (err) return res.status(500).json({ error: err });
    //Execute query
    Exercise.find(query)
      .limit(Number(limit))
      .select("description duration date ")
      .sort({ date: -1 })
      .exec(function (err, data) {
        if (err) return res.status(500).json({ error: err });
        let dataToUser = [];
        data.forEach(function (item) {
          dataToUser.push({
            description: item.description,
            duration: item.duration,
            date: new Date(item.date).toDateString(),
          });
        });
        res.json({
          _id: userinfo._id,
          username: userinfo.username,
          count: data.length,
          log: dataToUser,
        });
      });
  });
});
1 Like

Me too. Let me know if you fix(ed) this.

EDIT: I fixed this. The problem for me was not the log route but the exercise/add route. The test for that route passes but the route fails while doing the set up for the log test. Check your Glitch console (I used Morgan to log http errors). I was getting a 400 error on the exercise /add route during the log test.