Exercise Tracker, not sure why test 5 is not passing

Tell us what’s happening:
I am able to pass all tests except number 5

“I can retrieve a full exercise log of any user by getting /api/exercise/log with a parameter of userId(_id). App will return the user object with added array log and count (total exercise count).”

I am running out of ideas. Please take a look. I am probably missing something simple.

Your code so far

The glitch code is here:

EDIT: I looked up the tests for this exercise. They are here. I tried matching the expected output exactly but it still didn’t pass.
I pass this kind of json as the response:

{
"_id": "5eeda2dfae9abf0181cd6ad0",
"username": "fcc_test_15926318569",
"log": [
    {
    "username": "fcc_test_15926318569",
    "description": "push ups ",
    "duration": 9,
    "_id": "5eeef5d8b25c0404922cc981",
    "date": "Mon Jun 22 2020"
  }
],
"count": 1
}

This is the text of the test failure. It’s not timing out (a problem some people had).

// running tests
I can retrieve a full exercise log of any user by getting /api/exercise/log with a parameter of userId(_id). App will return the user object with added array log and count (total exercise count).
// tests completed

The GItHub repo is here:

The app is live here:

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36.

Challenge: Exercise Tracker

Link to the challenge:

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:

If you still don’t understand my explanation, get in touch.

I’m stuck on the same test 5, my log output looks fine, could you explain in detail about what the test is expecting when creating the exercise with date parameter undefined? I thought if date parameter was not provided we have to get and put the current date to that exercise and so I did. Thanks.

Your problem might be different. Check the server side log (on Glitch or whatever) after running the test. If you get any http errors post it here.

I got this log in Glitch after running the tests, I think it is something related to “date” field:

{ Error: User validation failed: exercises.0.date: Cast to date failed for value "Invalid Date" at path "date"
at MongooseError (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/error/mongooseError.js:7:1)
at ValidationError (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/error/validation.js:26:5)
at model.Document.invalidate (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/document.js:2579:32)
at EmbeddedDocument.invalidate (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/types/embedded.js:291:29)
at EmbeddedDocument.$set (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/document.js:1251:12)
at EmbeddedDocument._handleIndex (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/document.js:980:14)
at EmbeddedDocument.$set (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/document.js:921:22)
at EmbeddedDocument.Document (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/document.js:140:12)
at EmbeddedDocument [as constructor] (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/types/embedded.js:42:12)
at new EmbeddedDocument (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/schema/documentarray.js:115:17)
at CoreDocumentArray._cast (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/types/documentarray.js:109:12)
at CoreDocumentArray._mapCast (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/types/core_array.js:272:17)
at Object.map (native)
at CoreDocumentArray.push (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/types/core_array.js:670:21)
at CoreDocumentArray.push (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/types/documentarray.js:210:28)
Jump Toat /app/server.js:108:22
errors:
{ 'exercises.0.date':
{ CastError: Cast to date failed for value "Invalid Date" at path "date"
at MongooseError (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/error/mongooseError.js:7:1)
at CastError (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/error/cast.js:27:7)
at SchemaDate.cast (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/schema/date.js:341:11)
at SchemaDate.SchemaType.applySetters (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/schematype.js:1070:12)
at EmbeddedDocument.$set (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/document.js:1211:20)
at EmbeddedDocument._handleIndex (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/document.js:980:14)
at EmbeddedDocument.$set (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/document.js:921:22)
at EmbeddedDocument.Document (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/document.js:140:12)
at EmbeddedDocument [as constructor] (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/types/embedded.js:42:12)
at new EmbeddedDocument (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/schema/documentarray.js:115:17)
at CoreDocumentArray._cast (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/types/documentarray.js:109:12)
at CoreDocumentArray._mapCast (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/types/core_array.js:272:17)
at Object.map (native)
at CoreDocumentArray.push (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/types/core_array.js:670:21)
at CoreDocumentArray.push (/rbd/pnpm-volume/3ce2fe38-1c44-475c-9a6a-e323a5782590/node_modules/.registry.npmjs.org/mongoose/5.9.26/node_modules/mongoose/lib/types/documentarray.js:210:28)
Jump Toat /app/server.js:108:22
stringValue: '"Invalid Date"',
messageFormat: undefined,
kind: 'date',
value: 'Invalid Date',
path: 'date',
reason: [Object] } },
_message: 'User validation failed' }

Looks like you are getting a validation error from Mongoose. So your Schema/ model are not getting what they expect. Before you try to create a date from a string and pass it along you should check whether the string converts to a date or not. You’ll also need to handle the case when an empty string is passed from the form. Unfortunately, as I mention in my post above, you will also have to check if the request body even has that parameter. The FCC test tries to add an exercise without passing it at all.

Cast to date failed for value “Invalid Date” at path “date”

“Invalid Date” is what gets returned when you try to create a Date object with invalid input.

This Stack Overflow question might help:

Thanks for your answer! I have handled that error and apparently finished the whole project, but testing still show test 5 as “not passed”, even test 6 is OK now. I think is something related to the validation performed but I’m tracking now all operations of validation using console.log() and I think everything is OK on my project.

The part that I still don’t understand is that I’m not passing test 5 which is “I can retrieve a full exercise log of any user by getting /api/exercise/log with a parameter of userId(_id). App will return the user object with added array log and count (total exercise count).” But on debugging I’m realizing that this test apprently is not requested at any moment, I just can see 3 requests but with some optional parameters (from, to, limit) but I never see a request with pure “userId”.

This is my code in case you can have a look on it, thanks! https://glitch.com/edit/#!/comet-amusing-failing

I requested to join your project on glitch.
You can see exactly what tests are running here .

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

I also removed unimportant details (the timestamps).

You can also use the “preformatted text” tool in the editor ( </> ) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Thanks, I ran into this same wall as well. Was debugging all day yesterday and it was unhandled ‘undefined’ from the date parameter that was causing the issue.

You’re welcome. Glad it was useful.

Hello! I think I didn’t receive your request on glitch :frowning: . can you send again? I checked the tests running and now I am seeing that the problem is due to the undefined date in that test. I was just answering with a JSON saying that an undefined date was entered but I think the test is expecting to create the exercise anyways. How did you manage the undefined date?

No problem. We must be in distant time zones.

The answer to that is in my solution above :

If you go to the example project and check the instructions in README.md, it says:

I can add an exercise to any user by posting form data userId(_id), description, duration, and optionally date to /api/exercise/add. If no date supplied it will use current date.

So, check if the date parameter is undefined, if so, create a new date and pass that instead.

You are totally right, I changed that and I passed all tests, thanks so much for your help!

Good job! Good luck with the next one.

My issue passing test five was that I was using route parameters (like on the previous challenges) rather than query parameters, so I was requesting from and handling:
https://boilerplate-project-exercisetracker.michaeltandy.repl.co/api/exercise/log/5f6242494b91691c83a2b4cc
when I should have been handling
https://boilerplate-project-exercisetracker.michaeltandy.repl.co/api/exercise/log?userId=5f6242494b91691c83a2b4cc
Just leaving this here in case anyone else makes the same mistake…

1 Like

This is solution to exercise Tracker, I forked @OsakaStarbux’s code and then did a code refactor. Using Modern ES6 Syntax. It is all Ok and Passes all the Tests.

In case you are stuck somewhere, don’t hesitate to look through

Thank you for your comment! I initially overlooked it, but now I’ve realized that your mistake was the same as mine.

I should have been using req.query.userId not req.params.userId. :smiley: