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 ?
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 formatDate::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.
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})
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).
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.
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.
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().
In myfunction1 you are setting datec before declaring the variable: var datec=datec.toISOString().slice(0, 10)
Hi,
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 ?