Exercise Tracker Project Question

I’m on this project: https://www.freecodecamp.org/learn/back-end-development-and-apis/back-end-development-and-apis-projects/exercise-tracker

Right now I keep getting the error “TypeError: Converting circular structure to JSON” whenever I try accessing “/api/users/_id:/logs” but I’m not sure where the circular structure is coming from.

Link to code is here, issue is toward the bottom under “INCOMPLETE.”

Link: https://replit.com/@sssmsm/boilerplate-project-exercisetracker#server.js

I know that the exercise requires putting in date parameters to only show certain exercises periods as well, but I’m just trying to get the basic functionality down before getting to that.

Any help is greatly appreciated, thank you!

Remember, the find functions in mongoose are asynchronous. Now try to find the issue yourself. It’s right there.

I’m sorry but I don’t know what you mean by “asynchronous.” That didn’t come up during the FCC exercises or the previous projects.

Asynchronous functions are those which take some time to complete the execution. So, instead of returning the data, they either return a Promise or accept a callback function. Check mongoose docs for reference on how to use find functions.

Mongoose v6.0.13: (mongoosejs.com)

1 Like

Ah, I see! I’d never used “async” or “await” before, but they definitely helped here.

Another issue I was having was that I was trying to link up the “_id” of the User object with the “_id” of the Exercise object when they were auto-generated and different, so I created a “userid” field in the Exercise object to store the “_id” of the User and match them up that way.

Much appreciated! :slight_smile:

Instead of creating a separate document, you can nest them inside User to better signify their relationship. As mongodb is not a relational database, we cannot create foreign keys. But here’s an article that might help you.
Relationship in MongoDB - GeeksforGeeks

Happy coding :wink:

Hmm, I think something related to that is why my code is not passing the tests. Even though it produces the correct output, I don’t think it’s doing it in the way that the tests want.

Right now I’m getting a fail on: “The response returned from POST /api/users/:_id/exercises will be the user object with the exercise fields added.”

To pass that, do I have to somehow add the “Exercises” document into the actual “Users” document? I read through that link you posted, and while they have lots of output examples, I don’t see the code anywhere to actually make that happen.

It can be a way to do. But you can also add the exercise field manually. And the link I sent contains examples for structure of documents and not the code. You can look at the Subdocuments section of mongoose docs if you want. Hope that helps.

It seems I simply forgot to add the “userid” field in the Exercise schema creation section, and that’s what was causing the error. Appreciate the assistance though! :slight_smile:

Oh, Alright. Happy learning :smiley: