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.
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).
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.
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;
(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;
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;