I recently completed the Exercise Tracker and spent a fair amount of time thrashing which, based on other comments here, is not uncommon. The User Stories for this project are poorly written and the tests are notoriously vague. I figured I’d share my learnings to possibly help out the next folks who attempt this one.
Mongo Schema
- Mongo automatically adds Versioning (
__v
) andid
fields unless you explicitly disable them. Having these present will potentially break test 4 if you’re returning the Mongo Model directly.
Parameters
Parameters are not always passed in the URL. For the example, the HTML based schema exerciser uses a form with a POST query which sends empty fields as blank. The tests do not send a blank parameter when they do a GET/POST. This means that you need to both check to see if the parameter exists AND if it is blank before using it.
Review the Tests
The tests can be found here:
https://github.com/freeCodeCamp/freeCodeCamp/blob/6b3c61c73757d6ffe2ea332cbc44dad7948d1175/curriculum/challenges/english/05-apis-and-microservices/apis-and-microservices-projects/exercise-tracker.english.md
Understanding them can help fill in the user stories and save you time and heartache.
Test 4
The expected result must match this exactly:
const expected = {
username,
description: 'test',
duration: 60,
_id,
date: 'Mon Jan 01 1990'
};
duration
must be a number.
date
is the output from JavaScript’s Date.prototype.toDateString()
. You will need to transform the date stored by Mongodb. Note also that if your node.js environment has a timezone other than UTC, toDateString()
will modify the date. I suggest moment.js
as a library to simplify modifying/outputing dates.
Test 6
Filtering/Limiting
It is possible to filter and limit the log
portion of your User Schema using MongoDB’s aggregate
queries. You can build a $project
statement using the $filter
and $slice
aggregate functions respectively. You will need to pass in a JS Date
object when constructing the $filter
for the date. You may also need to $and
the to
and from
conditions together.
Alternatively, just use JavaScript array manipulation to get the same result. My preference long term would be to use the in-engine manipulation, since it is likely to be more performant and scale better. Consider the case where you had many thousands of results - it’s going to be faster to NOT send them over the network from your DB to Node then hold them in memory to filter.
Result
The result of Test 6 should be a single user object, not an array. It will contain an array however.
Testing and Debugging
The tests are run client side, which means that regardless of where you’re hosting/developing, you can run the tests so long as your computer has access to the URL. I was able to test with a localhost:3000
address.
Because the tests are occurring in-browser, you can view the browser console Network tab to see what is being sent and what is being returned. This, in conjunction with server side console.log
statements will give you good insight as to what is being sent and received by the tests.