Backend development and API projects : Exercise Tracker


I am currently working on the “Exercise tracker” exercise.
I get 3 incorrect items when testing my solution on freecodecamp.
From my point of view, the answer look like what is expected but it doesn’t pass.
Can you please provide me some help to solve my problems ?

Here is the link :

Thank you !

Your project link(s)

Your browser information:

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

Challenge: Exercise Tracker

Link to the challenge:

Hi, does someone has any idea about the problem I face ?
Thank you

Still need some help :slight_smile:

The problem is with the date field in your JSON responses. Currently you are returning:
date: "1990-01-02", but FCC expects date: "Mon Jan 01 1990". This is simply the format Date::toDateString() returns.

If you fix that for all responses, it will (most likely) pass all the tests.

Some feedback on your code. I noticed several (small) issues with your code. They will probably not result in failing tests. But I just wanted to point them out.

  1. a=newusername._id.toString();
    You are creating a global variable (i.e. no let, const or var). But there is no need for an intermediate variable, the following will work fine: res.json({username : newusername.username, _id : newusername._id})
  2. In GET /api/users you have an array users from your db, which you then loop over and push each individual user object into userMap. This way you end up with an array that is identical to users. You can just use res.send(users).
  3. In POST /api/users/:_id/exercises you have the following line: myfunction1(Username,res,_id, elements). A function should always have a name that reflects what it does. Also, you are passing Username to the function, but Username is already in the scope of the function. So there is no need to pass it to the function.
  4. In POST /api/users/:_id/exercises you have the following line: elements=req.body. Which again is a global variable, since you are missing const.
  5. The most important issue you might want to fix: you are using Username.find({}) to get all users and then you loop over these users to find one with the correct user id. But instead you should use Username.findById().
  6. In myfunction1 you are setting datec before declaring the variable: var datec=datec.toISOString().slice(0, 10)
1 Like

Thank you very much for your complete answer !
It is very interesting to have your feedback about my code. I will take your comments into consideration.
However, I have a question as I still don’t manage to validate the test.
I have modified the Date format as you suggested it to me but I still get the following problem :
" The date property of any object in the log array that is returned from GET /api/users/:id/logs should be a string… Use the dateString format of the Date API."
When I check, I see that the format is correct and that is a string.
Do you have any idea why I get this error message ?

Thank you very much or your support !

What happens if you POST a new exercise without a date? Or what if date is some random text (e.g. "asdjfladkf") that cannot be resolved to a date?

Note in both cases new Date(undefined) or new Date("asdjfladkf") will return "Invalid Date" instead of a Date object.