Possible bug in Exercise Tracker challenge?

I’m working on the Exercise Tracker challenge which is part of the back-end developer section. For the “POST an exercise” portion, I have coded the following route:

app.post('/api/users/:_id/exercises', (req, res) => {
  const _id = req.params._id

This works fine when I hit the endpoint from Postman or from the provided test form with a value in the URL for the _id parameter.

However, when running the challenge tests, the _id parameter is always coming in as undefined. E.g.,
if I console.log(req.params) at the top of the method, it shows:

params { _id: 'undefined' }

I also log the URL and get:

url /api/users/undefined/exercises

Is there a problem with the challenge tests that needs to be addressed or is it just me?

Stay safe; be well.

It is a form submit. If you use the correct middleware (urlencoded or body-parser) it will be on req.body.

Get Data from POST Requests

Thanks, lasjorg… I’m not convinced that is the issue – here’s why:

  1. I am using body-parser/urlencoded middleware to support form input.

  2. The URL that is sent by the test code shows undefined; as noted in my original post, the URL is /api/users/undefined/exercises – no value for the _id.

  3. Most telling: when I submit a request from the sample form included in the replit workspace, it works just fine (and there is a _id value in the URL). And yes, in that case there is an _id value in the body.

There’s no way to really debug this without a live project (repl.it) to work with. The tests are working, without issue (I just verified this on one of my projects).

The tests use your routes to create users and exercise entries, so if one route is working well enough to pass the tests but not in complete compliance with the specification, you may pass the “create user” test but not return an appropriate _id, which then leads to trying to add exercise records to user undefined, for instance.

Without a live project, there’s no real way to determine the problem. You should be able to track it down by logging all the route inputs and all the route responses from all the routes and then look at what happens as the tests create a user and then try to add exercise records to that user.

Thanks very much, @jeremy.a.gray (and @lasjorg too)… When I ran my tests this morning after more debugging, everything worked as expected: i.e., _id was defined and the create-exercise tests pass. Turns out the bug was mine (of course), although not a body-parser issue. Looking back at the changes I had made earlier, your comment Jeremy that "… you may pass the “create user” test but not return an appropriate _id" was what was going on.
So… my bad :blush: