Exercise Tracker Help - GET request to '/api/users/:_id/logs' to retrieve a full exercise log

Tell us what’s happening:
Test 9 ( You can make a GET request to /api/users/:_id/logs to retrieve a full exercise log of any user.) to Test 15 Fail:

But Test 16 Passes ( You can add from , to and limit parameters to a GET /api/users/:_id/logs request to retrieve part of the log of any user).

I have the response json returning all the expected fields/values and everything is formatted correctly, as far as I can tell. I currently have it displaying like the example code shows. I’ve also tried looking at other peoples forum posts / examples and tried different methods of searching + filtering, but still can’t get these tests to pass.

I’ve been stuck on this for a few days and really need help!

My Code for the GET request:

app.get('/api/users/:_id/logs', function(req,res){

  userModel.findById(req.params._id, (err,user)=>{
    if(err) return res.send('Error');
    if(!user) return res.send('Unknown User');
    
    exerciseModel.find({user_id:req.params._id},{_id:0,user_id:0,__v:0}, (err,log)=>{

      // if 'from' & 'to' query exist, format array.
      if(req.query.from && req.query.to){
        log = log.filter(e=> new Date(e.date) >= new Date(req.query.from) && new Date(e.date) <= new Date(req.query.to))
      }

      // if 'limit' query exist, format array.
      if(req.query.limit){
        log = log.splice(0,req.query.limit)
      }
      
      // format date.
      log = log.map(e=>{
        return ({
          description: e.description,
          duration: e.duration,
          date: new Date(e.date).toDateString()
        })
      })

      res.json({
        username: user.username,
        count: log.length,
        _id: user._id,
        log: log
      })
    })
  })

})

Your project link(s)

solution: boilerplate-project-exercisetracker - Node.js Repl - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

Challenge: Back End Development and APIs Projects - Exercise Tracker

Link to the challenge:

These tests use the default date and are failing.

These tests use a set date and do not fail. So, it’s your date handling which means it must be in the POST route and not the GET since that is where the default date is set. Run the tests with the browser console open and you’ll see all the 400 errors.

Logging the input and the date handling in the POST yields

req.body: {"description":"test","duration":"60"}
req.params: {"_id":"630bd90d0afa5c5430e421ae"}
req.query: {}
i think i received an invalid date

which means your date code is not correctly implementing the part of the spec concerning dates (no date, which is valid, is not the same as an empty string date…) when posting exercises (despite test 7 passing as it provides a date).

1 Like

Thank You, Thank You, Thank You! - This was exactly what wasn’t working.

I didn’t even think about checking my browser console to check what was happening, but you were spot on.

I had an ‘if’ statement checking for an empty date string in my POST request for exercises, rather then checking if a ‘date’ value exists in the request body. Fixing it got all my tests on the project to pass.

You’re the best!

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