Back End Development and APIs Projects - Exercise Tracker

Can anyone be my life-saver to help me figure out the problem? I manually tested my code with new users, exsiting users, new log with user ID exsiting in database, new log with Id not exsiting in database. All got expected result for both json response and new doc in database. There’s no error message in console. But it does not allow me to pass the test. So what should I do to solve this problem?

Your project link(s)

solution: boilerplate-project-exercisetracker-2 - Nix (beta) Repl - Replit

Your browser information:

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

Challenge: Back End Development and APIs Projects - Exercise Tracker

Link to the challenge:

When I test it manually, I can post new log to the exercise area with no problem at all, but when freecodecamp tests my code, it cannot post new log to exercise. I don’t know the reason. It shows the test does pass only, no error message in the console at all.

This is from your Replit code, for the POST /api/users/:_id/exercises request:

app.post('/api/users/:_id/exercises', async (req, res) => {
    const inputId = req.body[':_id'];
    ...

The req.body[':_id']; is not the correct way of reading the request parameter. The :id in the path /api/users/:_id/exercises represents a request parameter.

How to get that request parameter value, is an issue for you. I suggest refer your course notes, or/and lookup online at ExpressJS for Route Parameters - to know how to use it in your app.

I think the console would tell me that I got null or undefined if that’s the problem. No such error message in console.

Thanks. I know we can get the id with req.params._id as well, but my way of doing this is also correct. You can check the name of the id area in the exercise area, the name there is the way how I did. And what’s more important, I successfully got the doc I want when I do the test on my local host as well as on in manual test in replit. If I cannot get the doc I want, that would be exactly where the problem is.

Here is a guide about ExpressJS Routing - the sub-topic Route Parameters is of interest in this case.

Thanks. I fixed it as the way you mentioned. It still does not let me pass the tests. Most of them are in the retrieve log part.

in the retrieve logs tests, it says a get request to /api/users/:_id/logs some where, /api/users/:id/logs in other requirements. Are they saying the same thing?

Yes, its the same thing. The value in the route path :id or :_id represents a request parameter, just different name. The value you supply to it is the _id value of the users collection document. Note that, when you create a document in the MongoDB collection, the _id field is added by default (automatically) and the field type is of ObjectId.

Yes. The _id is added automatically. Would you please help me have a look at my code for the retrieving logs part? I can get all the logs when I manually test it in replit. And in the requirements, two of them are very similar, I’m nor sure if they are asking for the same thing or not. One says get the full exercise record, and the other says with a log array of all exercises added. I personally understand them as the same thing.

  • A GET request to /api/users/:id/logs will return the user object with a log array of all the exercises added.

  • Waiting:Each item in the log array that is returned from GET /api/users/:id/logs is an object that should have a description, duration, and date properties.

  • Waiting:The description property of any object in the log array that is returned from GET /api/users/:id/logs should be a string.

  • Waiting:The duration property of any object in the log array that is returned from GET /api/users/:id/logs should be a number.

  • Waiting:The date property of any object in the log array that is returned from GET /api/users/:id/logs should be a string. Use the dateString format of the Date API.

  • Waiting:You can add from, to and limit parameters to a GET /api/users/:_id/logs request to retrieve part of the log of any user. from and to are dates in yyyy-mm-dd format. limit is an integer of how many logs to send back.

The above are the tests with expected results from the challenge. Which one is not clear to you?

Note all of them are regarding one route, GET /api/users/:id/logs.

I can only suggest you try to code each of the tests at a time, and get through.

1 Like

The first requirement that I didn’t pass. Is it the same as the one that two ahead of this in all the requirements? The two ahead that is retrieve a full exercise log of any user which I thought is the same thing as the first one I didn’t pass.

You can make a GET request to /api/users/:_id/logs to retrieve a full exercise log of any user.t

    • A GET request to /api/users/:id/logs will return the user object with a log array of all the exercises added.*

Are these two requirements asking for the same thing?

Thank you for your help mate. I finally sorted it out. Freecodecamp injected quite a lot undefined data into their test cases which I didn’t consider. After I put undefined into consideration, I sorted out the problem very quickly.