Back End Development and APIs Projects - Exercise Tracker

Tell us what’s happening:
I’m at my wit’s end, and I cannot figure out why my project is failing on the following step;

The response returned from POST /api/users/:_id/exercises will be the user object with the exercise fields added.

All other steps for this requirement pass successfully. Only this step seems to be failing. I can’t figure out why this would be the case. If someone could assist me, I would greatly appreciate it.

Also, the requirements for these exercises are very vague and leave room for misinterpretation. If FreeCodeCamp could help us make the requirements for these exercises clearer without ambiguity, it would help us a great deal!

I am sending my project link to my Replit source code. No feedback or reasons are provided as to why/where this step fails.

Your project link(s)

solution: nodejs-api-exercise-tracker - Replit

githubLink: nodejs-api-exercise-tracker - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36

Challenge: Back End Development and APIs Projects - Exercise Tracker

Link to the challenge:

I find your code extremely complicated, sorry!

What I have found though:

  1. Creating a new user works, but your repl crashes if you try to create a username which already exists. (This doesn’t fail the project of course but is an issue worth fixing).
  2. Adding an exercise to a user also works, in that it creates a record successfully in the database. However it does not return the required response, which is the user object containing the exercises array (hence the failed test). Instead, it appears to attempt to return only the array, which comes up empty for some reason (via getExercisesByUser). Perhaps something is activating a subsequent GET request after the POST?

There’s no reason why you can’t have separate modules for controllers, routes etc but I find your code structure impossible to follow, as I’m constantly ducking and diving in and out of various circuitous functions and modules as I’m trying to figure out the workflow of a particular process.

Perhaps that’s just me though… As a rule, I try to keep things as simple as possible. My version of this project was around 120 lines of code in total, all contained within the server.js file. I could have tidied it up a little by having a separate routes file perhaps, but it’s such a small project it didn’t seem worthwhile.

Hi Doug

Thank you for your response on my issue!

However, I find it strange that from my side, I am not encountering some of the issues that you seem to be getting.

when I try to create a user that already exists, I receive a response that the user already exists;

unnamed

(Although I do see a message logged to my Console; “Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client”)
But that doesn’t affect the code execution at all, though.

Secondly, when I add a new exercise to the User, I do get a response back from the API;

Unless it is just an issue with the Replit itself? I can run the API from my local host, and it works as required, so I’m mystified!!!

Thank you

I can’t test your Replit as it keeps timing out, not sure why because if I fork it, it runs just fine.

You have the wrong data type for duration.

I finally figured out what the issue was with my code and why it was failing on the step;
“The response returned from POST /api/users/:_id/exercises” will be the user object with the exercise fields added.

I was sending the output of the Exercise object incorrectly. I was sending it as;

return res.status(200).json({
    _id: user._id,
    username: user.username,
    exercise: req.body.description,
    duration: parseInt(req.body.duration),
    date: formattedDate,
  });

It should be;

return res.status(200).json({
        _id: user._id,
        username: user.username,
        description: req.body.description,
        duration: parseInt(req.body.duration),
        date: formattedDate,
      });

I was passing the description of the Exercise object incorrectly. I fixed that issue, and all tests passed successfully :sweat_smile: :upside_down_face:

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