Exercise Tracker - 5th test : Unable to pass despite trying many things

In a normal tone of voice I speak:

I’ve tried using req.query.userId and req.params.userId and many other things, and I still can’t get my code to pass the 5th test.

I wrote:

    log: log,
    count: log.length

and it does not work even though when I test it, it returns the log as an array and the count value, log.length.

I’ve tried many things and none of them have worked.

I’ve read several, if not all the topics, related to this, and have tried…, and still unsuccessful. Maybe I’m missing something. ???

The 5th test says to return the user object with added log and count so I’ve also tried, among other things:


And still the test does not pass… It’s bumming me out.

Here is my repl.it:

1 Like

Hello there,

The 5th test only has 2 conditions:

const logRes = await fetch(url + `/api/exercise/log?userId=${_id}`);
          if (logRes.ok) {
            const { log } = await logRes.json();
            assert.equal(1, log.length);

So, provided log is an array, and has length === 1, the test will pass.

I have seen some campers fail this, because, somewhere within their logic, two or more logs are added to the DB.

Hope this helps

I also notice you have added in the password fields to the forms. Does your app use the password logic before serving the log data? The tests do not give a password.

I sure the password field isn’t causing it to fail because it is not required, and because the 4th test passes despite the password field.

How would two or more logs be added?

My code for the 4th test is in database.on("open", function addToLog(req, res) { .... }); and my code for the 5th test is in ``` database.on(“open”, function retrieveLog(req, res) { … });

What does the 5th test mean: “App will return the user object with added array log and count (total exercise count).”

Here is an example output:

{"_id":"5fbc00212e4b4c2d4f3f78b1","username":"test_12345","count":1,"log":[{"description":"Test 1","duration":23,"date":"Mon Nov 23 2020"}]}

Does it have to be formatted exactly that way?

The test is exactly as I pasted above; all that needs to be there is:

  1. An object must be returned
  2. The object must contain a property log
  3. log must be an array
  4. log.length === 1

From what I can see (quick glance), count does not even need to be there.

@Sky020, I ran the test, and then I went into my MongoDB and manually grabbed the 4 _ids inputted by the FCC test run.
1st one: empty array, count: 0
2nd one: count: 1,
3rd one: count: 0,
4th: count: 2

If the test is testing for assert.equal(1, log.length); does that mean that it is expecting the log length of every entry to be 1?

But the test says there must be a "user object with added log array and count (total exercise length)

Here are the tests: freeCodeCamp/exercise-tracker.md at master · freeCodeCamp/freeCodeCamp (github.com)

This is the test in question:

Test 5
const url = getUserInput('url');
      const res = await fetch(url + '/api/exercise/new-user', {
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        body: `username=fcc_test_${Date.now()}`.substr(0, 29)

      if (res.ok) {
        const { _id, username } = await res.json();
        const expected = {
          description: 'test',
          duration: 60,
          date: new Date().toDateString()

        const addRes = await fetch(url + '/api/exercise/add', {
          method: 'POST',
          headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
          body: `userId=${_id}&description=${expected.description}&duration=${expected.duration}`
        if (addRes.ok) {
          const logRes = await fetch(url + `/api/exercise/log?userId=${_id}`);
          if (logRes.ok) {
            const { log } = await logRes.json();
            assert.equal(1, log.length);
          } else {
            throw new Error(`${logRes.status} ${logRes.statusText}`);
        } else {
          throw new Error(`${addRes.status} ${addRes.statusText}`);
      } else {
        throw new Error(`${res.status} ${res.statusText}`);

To walk through what is happening:

  1. A new user is created
  2. A new exercise is added ({description: "test", duration: 60})
  3. The log is fetched and tested (assert)

When I test your app manually, I am unable to retrieve the logs of the user I create:

I switched from :userId param to query param because the test shows /api/exercise/log?userId=${_id}

And now when I manually input /api/exercise/log?userId=5fbbfd93984dc501ceaa47cf

I get

{“log”:[{“description”:“test”,“duration”:60,“date”:“Mon Jan 01 1990”},{“description”:“test”,“duration”:60,“date”:“Tue Jan 02 1990”}],“count”:2}

… By the way, thank you for doing your best to help me.

Just to clarify, is that one of the fcc_test users?

Yep: fcc_test_16061556686

This one has 2 items in the array.

But, that one wont work now because I deleted the database entries so that I could try something else.

But, you can use this (which I left in there to have something to test):
id: 5fb871bf03226800fb206cbc
username: “username1”
password: “password1”
log: Array
__v: 0

@Sky020, I just read another post about the 5th test, and added || date === undefined to my ``` if (date === “”) condition for the date since:

OsakaStarbux said " I was so focussed on passing test 5 that I couldn’t see the real problem. In a perfect world the tests would only test one thing at a time but in this case test 5 bundles the exercise log and unexpected user input (for another route, not the log route). This is what fooled me. I spent ages guessing what the log output should look like when it was fine.

The real problem was the test setting up the user and exercises ready to be tested. After a user is created the test code hits the exercise/add route to create a log but one of those requests failed because the date parameter is undefined. You need to handle that or the log will not be what the test expects (the exercise doesn’t get created).

This is a little unfair since this can only fail because your form is not being used. The test actually builds a bad request because it can. Also, test 4 (for adding exercises) will pass leaving you with a false sense of security. I think the tests need fixing. I’ll have a go at that once I feel confident.
EDIT: I submitted a pull request for this issue:


Fix missing date parameter


opened Jul 15, 2020

OsakaStarbux OsakaStarbux

+2 -2 "

The link to the post I read:
Exercise Tracker, not sure why test 5 is not passing - JavaScript - The freeCodeCamp Forum


In general, it is most common to see:

const dateToAdd = req.param.date ? req.param.date : new Date();
// Something like that. Point being: if (date) {...}

Thank you, for explaining the problem. It will be useful for other campers.

Hope that helps

You’re welcome.
Thank you for your help and for that information.
I just modified the Hint for this challenge. I hope it gets used.

Exercise Tracker 4th, 5th and 6th Challenges Hints - Contributors - The freeCodeCamp Forum