Exercise Tracker Last Test Failing

Tell us what’s happening:
Describe your issue in detail here.
When I manually test the use of query strings it seems to be working however the last test isn’t passing. I’d greatly appreciate any insight on whats wrong and/or a way to see exactly what the test case is doing.
Feedback on style would also be welcomed.
Thanks for your help

Your project link(s)
Replit link:

solution: https://replit.com/@cat403/exercisetracker

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Firefox/91.0

Challenge: Exercise Tracker

Link to the challenge:

1 Like

How to debug is easy. Log all the inputs and responses for the misbehaving route:

app.get("/api/users/:_id/logs", (req,res)=>{
  console.log(req.body);
  console.log(req.params);
  console.log(req.query);
  
  if(req.params._id.match(/^[0-9a-fA-F]{24}$/)){
    Athleat.findById(req.params._id,(err, person)=>{
      if (err) return console.error(err);
      if (!person) return res.json({error: "id not found"});
      let sortedLog = person.log.sort(
        (objectOne, objectTwo) => {
           return new Date(objectTwo.date).getTime() - new Date(objectOne.date).getTime();
        });
      if (new Date(req.query.from)!="Invalid Date"){
          const minDate = new Date(req.query.from).getTime();
          sortedLog = sortedLog.slice(sortedLog.findIndex((object)=>
          minDate <= new Date(object.date).getTime()),)
        }
      if (new Date(req.query.to)!="Invalid Date"){
          const maxDate = new Date(req.query.to).getTime();
          sortedLog = sortedLog.slice(0,sortedLog.findIndex((object)=>
          new Date(object.date).getTime() >= maxDate)+1)
        }
      if (req.query.limit && parseInt(req.query.limit)>0){
        sortedLog = sortedLog.slice(0, parseInt(req.query.limit))
      }
      
      console.log({_id:person._id, username:person.username, count: sortedLog.length, log: sortedLog})
      return res.json({_id:person._id, username:person.username, count: sortedLog.length, log: sortedLog})
        
    });
  }
  else {
   console.log({error: "invalid id"})
   return res.json({error: "invalid id"})
  }
 
});

Or in other words, log req.body, req.params, and req.query at the beginning of your route and then log every response the route will send by duplicating a return res.json(...) with a console.log(...). This generates with the fCC tests

}
{ _id: '612d75d6cd47bb41d7a0b5ae' }
{}
{
  _id: new ObjectId("612d75d6cd47bb41d7a0b5ae"),
  username: 'fcc_test_16303692386',
  count: 1,
  log: [
    {
      description: 'test',
      duration: 60,
      date: 2021-08-31T00:00:00.000Z,
      _id: new ObjectId("612d75d6cd47bb41d7a0b5b1")
    }
  ]
}
{}
{ _id: '612d75d7cd47bb41d7a0b5b5' }
{}
{
  _id: new ObjectId("612d75d7cd47bb41d7a0b5b5"),
  username: 'fcc_test_16303692390',
  count: 1,
  log: [
    {
      description: 'test',
      duration: 60,
      date: 2021-08-31T00:00:00.000Z,
      _id: new ObjectId("612d75d7cd47bb41d7a0b5b8")
    }
  ]
}
{}
{ _id: '612d75d7cd47bb41d7a0b5bc' }
{ from: '1989-12-31', to: '1990-01-03' }
{
  _id: new ObjectId("612d75d7cd47bb41d7a0b5bc"),
  username: 'fcc_test_16303692395',
  count: 0,
  log: []
}

Pay attention to the dates, since they are all from when the tests run and not from when the exercise should be recorded, which means the filtered log is not returning any results and the test expects some results.

Thanks for the logging suggestions. I’m not certain why the previous function failed to returned anything in the fcc test but was working fine with the id I was testing but after refactoring I managed to pass the test.

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