Exercise tracker: Last user story - Filter exercises by date

Hi there,

I’ve been stuck here for hours and I can’t find they to solve it. Although it does what it is supposed to do, I can’t complete the last challenge. I thought it could be something related with the Date objects but to be honest I don’t know because it is working properly.

Here is my code:

app.get('/api/exercise/log', (req,res) => {
  if(req.query.userId) {
    const userId = req.query.userId
    const user = log.find( user => user._id == userId)

    if(req.query.from && req.query.to) {
      const fromDate = new Date(req.query.from)
      const toDate = new Date(req.query.to)
      const limit = req.query.limit && parseInt(req.query.limit)
      
      const result = user.log.filter( ex => new Date(ex.date) >= fromDate && new Date(ex.date) <= toDate)
      const resultToLog = limit ? result.slice(0,limit) : result
      const toLog = {
        ...user,
        from: fromDate.toDateString(),
        to: toDate.toDateString(),
        count: resultToLog.length,
        log: resultToLog
      }

      res.json(toLog)
  } else {
   if(!user) {
      res.send('There is no an user with that Id')
    } else {
      res.json(user)
    }
  }
  } else {
    res.send('You need to provide an userId')
  }
})

Repl.it project link

Query to:

https://boilerplate-project-exercisetracker.inked.repl.co/api/exercise/log?userId=3184b0f2-8094-4774-a8c1-38cde50cb268

Response:

{"_id":"3184b0f2-8094-4774-a8c1-38cde50cb268","username":"inked","count":3,"log":[{"description":"abs","duration":34,"date":"2020-02-23T00:00:00.000Z"},{"description":"abs","duration":34,"date":"2020-12-05T00:00:00.000Z"},{"description":"abs","duration":34,"date":"2021-02-11T21:21:07.930Z"}]}

Query to:

https://boilerplate-project-exercisetracker.inked.repl.co/api/exercise/log?userId=3184b0f2-8094-4774-a8c1-38cde50cb268&from=1900-10-05&to=2021-02-28

Response:

{"_id":"3184b0f2-8094-4774-a8c1-38cde50cb268","username":"inked","count":3,"log":[{"description":"abs","duration":34,"date":"2020-02-23T00:00:00.000Z"},{"description":"abs","duration":34,"date":"2020-12-05T00:00:00.000Z"},{"description":"abs","duration":34,"date":"2021-02-11T21:21:07.930Z"}],"from":"Fri Oct 05 1900","to":"Sun Feb 28 2021"}

Query to:

https://boilerplate-project-exercisetracker.inked.repl.co/api/exercise/log?userId=3184b0f2-8094-4774-a8c1-38cde50cb268&from=1900-10-05&to=2021-02-28&limit=1

Response:

{"_id":"3184b0f2-8094-4774-a8c1-38cde50cb268","username":"inked","count":1,"log":[{"description":"abs","duration":34,"date":"2020-02-23T00:00:00.000Z"}],"from":"Fri Oct 05 1900","to":"Sun Feb 28 2021"}

Query to:

https://boilerplate-project-exercisetracker.inked.repl.co/api/exercise/log?userId=3184b0f2-8094-4774-a8c1-38cde50cb268&from=2021-02-05&to=2021-03-01

Response:

{"_id":"3184b0f2-8094-4774-a8c1-38cde50cb268","username":"inked","count":1,"log":[{"description":"abs","duration":34,"date":"2021-02-11T21:21:07.930Z"}],"from":"Fri Feb 05 2021","to":"Mon Mar 01 2021"}

Link to challenge

Hey there,

You are correct about this. We are discussing a solution to better informing Campers, but the date returned must be in the toDateString() format:
Flesh-Out Exercise Tracker User Stories and Typo · Issue #40405 · freeCodeCamp/freeCodeCamp (github.com)


Just to mention, I am unable to get your Repl running - this might be something to do with Repl, though, and will be rectified, eventually.

Hope this helps

1 Like

Thank you very much, I will look into it immediately. Have a nice weekend!

I’ve been taking a look into this and reviewing the exercise model to match the requirements but still can’t pass the last user story.

const toLog = {
    description: description,
    duration: parseInt(duration),
    date: date ? new Date(date).toDateString() : new Date().toDateString()
  }

Just to point out: It is expected you will use a database for the project.

The issue is here:

} else {
      if (!user) {
        res.send('There is no an user with that Id')
      } else {
        res.json(user)
      }
    }

If I send a userId, and a limit, it does not do anything with the limit.

Hope this helps

1 Like

Thank you very much for reviewing and looking into this. It is makes more sense to use a database that using the server itself to hold the data. I hope it is pointed out in the challenge if they update it. Thank you again.

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