Exercise Tracker

Hey everyone, I’m having a hard time passing a few of the remaining tests of the exercise tracker.

I pass all other get and post tests and even the first and last GET /api/users/:_id/logs, but not the ones in between. I’ve been trying to recreate it and from what I can tell, I mostly have it, if not, give back the correct properties and values.

Please let me know if you see the problem, feedback is welcome.

const minDate = new Date(-8640000000000000);
const maxDate = new Date(8640000000000000);

app.get("/api/users/:_id/logs", (req, res) => {
  const userId = req.params._id;
  let fromDate = new Date(req.query.from);
  let toDate = new Date(req.query.to);
  let logLimit = req.query.limit;
    
  User.findOne({ _id: userId }).select({ "log._id": 0, __v: 0 }).exec((err, userData) => {
    if (err) return res.json(err);
    if (isNaN(logLimit))
      logLimit = userData.log.length;
    
    let filteredLog = userData.log.filter(obj => {
      let excDate = new Date(obj.date);
      return excDate >= (isNaN(fromDate) ? minDate : fromDate) && excDate <= (isNaN(toDate) ? maxDate : toDate);
    }).slice(0, Number(logLimit));
    
    userData.log = filteredLog;
    userData.count = userData.log.length;
    
    userData = userData.toObject();
    userData.to;
    userData.from
    
    if (!isNaN(fromDate))
      userData.from = fromDate.toDateString();
    if (!isNaN(toDate))
      userData.to = toDate.toDateString();
    res.json({
      _id: userData._id,
      username: userData.username,
      from: userData.from,
      to: userData.to,
      count: userData.count,
      log: userData.log.slice()
    });
  });
});

Log the route inputs and all the route responses while running the tests so you can monitor what the code is actually doing. If that doesn’t work by just logging from your GET logs route, do it from all routes since the problem may reside elsewhere.

If you can’t find the problem by logging, then you’ll have to post a repl so that all the code is available and others can run it to debug.

Thank you for responding, I’ve been busy lately and decided to take a closer look. This is my repl if anyone would like to take a look. As far as I can tell I am meeting the requirements and getting the same results as the freeCodeCamp exercise tracker. I’m going to move on to the next project and swing back around and attempt to recreate it from scratch, if anything for practice. I will console log the inputs and route responses before attempting .

https://boilerplate-project-exercisetracker.masonruebel.repl.co

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.