Back End Development and APIs Projects - Exercise Tracker

Tell us what’s happening:

Hi,

I’m stucked on /api/users/:_id/logs. All tests on that endpoint are false and I have no idea how I can fix them.

You can make a `GET` request to `/api/users/:_id/logs` to retrieve a full exercise log of any user.
... etc...

When I try with my records I get this json :

// https://boilerplate-project-exercisetracker.paulpiazza.repl.co/api/users/63b4226cff12a17f977dff77/logs

{
  "_id": "63b4226cff12a17f977dff77",
  "username": "paul",
  "count": 1,
  "log": [
    {
      "description": "ici teste 1",
      "duration": 2,
      "date": "Mon Dec 26 2022"
    }
  ]
}

Why my endpoint doesn’t pass the test ?

This is my endpoint :

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

    const idUser = req.params._id

    const from = req.query.from || 0
    const to = req.query.to || Date.now()
    const limit = req.query.limit

    User.findById(idUser).then((user) => {

      if (!user) res.status(404).json({ error: `No user found with id:${idUser}` })

      return Exercise
        .find({ username: user.username })
        .select({_id: 0, description: 1, duration: 1, date: 1 })
        .sort({ date: -1 })
        .limit(limit)
        .where('date').gte(from).lte(to)
        .exec()
        .then((exercises) => {
          if (!exercises) res.status(404).json({ error: `No exercises found for ${user.username}` })
          
          res.json({
            _id: idUser,
            username: user.username,
            count: exercises.length,
            log: exercises
          })

        })

    }).catch(err => res.status(500).json({ error: "Server Error. Please try later." }))
  })

I will be happy to have any advice on my code.

Your project link(s)

solution: boilerplate-project-exercisetracker - Replit

Your browser information:

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

Challenge: Back End Development and APIs Projects - Exercise Tracker

Link to the challenge:

Will this bit of the POST exercise route

      const newLog = new Exercise(
        {
          username: user.username,
          description: req.body.description,
          duration: Number(req.body.duration),
          date: Date.parse(req.body.date)
        }
      )

handle undefined dates according to the specification?

If you look in the browser network tab when submitting you can see the POST before the log GET is returning a 500 so it never does the log GET (because that is how the tests are set up).

Your User.findById is throwing and your catch is running which you can tell by looking at the response.

{"error":"Server Error. Please try later.","data":{"errors":{"date":{"stringValue":"\"NaN\"","valueType":"number","kind":"date","value":null,"path":"date","reason":{"generatedMessage":true,"code":"ERR_ASSERTION","actual":false,"expected":true,"operator":"=="},"name":"CastError","message":"Cast to date failed for value \"NaN\" (type number) at path \"date\""}},"_message":"Exercise validation failed","name":"ValidationError","message":"Exercise validation failed: date: Cast to date failed for value \"NaN\" (type number) at path \"date\""}}

As said, you are not handling the optional date.

Thanks a lot !! I didn’t notice that the date is optional.

Your github link is pretty interesting for understanding testing. I think I will continue with TDD.