Exercise Tracker Code works as required, (up to test 4)but not passing test 4

I’ve been able to update the db as required, but when I submit for tests, I pass only the first three. This is the console output:
throw er; // Unhandled ‘error’ event

TypeError: Cannot read property ‘username’ of null
at /home/runner/boilerplate-project-exercisetracker/server.js:98:35

The username is available because I log it on line 99.
I looked at the test for this one and I see that this may be where the error is happening:
if (res.ok) {
const { _id, username } = await res.json();
const expected = {
description: ‘test’,
duration: 60,
date: ‘Mon Jan 01 1990’

because I am using findByIdAndUpdate() and I am only searching for the _id.
What do Ineed to be looking at to pass this test? Is username needed at this point in the test if the outcome has the username available later?
Very confused.

Your project link(s)
solution: https://replit.com/@carolmurphy/boilerplate-project-exercisetracker

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:

Hello there,

I might be forgetting something, but what stands out is this:


Hope this helps

Just noticed the order of the params in the ‘expected’ variable of the test. If I change the order my keys inside my object that might do something.
[’:_id’] is the only way I found to code the :_id key, because the leading semicolon caused an error in server.js and it wouldn’t run. It returns the _id value and updates the db like I want it to, it looks weird though.

Hey again,

I had some time to properly go over this:

The order should not matter.

The issue is, you are trying to get the _id out of the body, when it is in the parameters of the request. Also, I have tested, this would not work, anyway: req.body[':_id'] → If _id existed on body, it would be req.body['_id'], or req.body._id

Also, there are some things you can clean up:

  • The date should be required in the Schema, because, even though your API does not require it to be sent, it is always required in a DB READ:
date: {type: String, required: false}
  • It is confusing having the newUser variable with the same handle as the newUser parameter:
newUser.save((err, newUser) => {
  • For both /api/users and /api/users/:_id/exercises, you need data handling, when the DB find calls do not find anything. That is, the callbacks do not have an err parameter, when they do not find anything - it is only used when there is an error (something went wrong in the process). **This is why your app is crashing, when it cannot find a user with _id: undefined (User.findByIdAndUpdate(req.body[':_id'],)
  • Why does the api/users/:_id/exercises route always return an object with a new date? You are welcome to do this, but why not store in the user document the date in the format you want to return it?

Hope this helps.

Thanks for looking at this. This kind of feedback is really helpful, because it helps me focus my attempts at troubleshooting my code, instead of just throwing things out there in scattershot fashion. I will have to go over each point later when I have more time (I’m just starting work)
I figured since :_id was the first field in the form, it would be retrieved with the req.body. I need to have a closer look at the difference between request and request.body.
Thanks again!

1 Like

Keep forgetting about params…
Swapped it for req.body, and test #4 passes!
Thanks again. Now to look at your other suggestions.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.