Back End Development and APIs Projects - Exercise Tracker

Tell us what’s happening:
Describe your issue in detail here.

I am cannot find the issue with the failing test error here:

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

When I send a POST request to create an exercise I get the following back as the response in Postman. It looks correct but there has to be something wrong somewhere in my code.

  {
     "_id": "641341277e3511cdd892f765",
     "username": "Susan",
     "description": "Swimming",
     "duration": 15,
     "date": "Sun Mar 19 2023"
}

Your project link(s)
project link: Exercise Tracker FCC - 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:

Log your IDs for the user and the exercise and remember that the spec says to return the user information (user name and ID) in the JSON returned from the POST exercise route.

1 Like

Code redone. Right off hand, I didn’t see a way to edit my post but I have change the code drastically since the last version. I am still getting errors for “user object with the exercise fields added.”, date string the last one, the filter. The filter isn’t inlcuding the last date I give it so I know there issues with that. “user objects with fields” has me stumped.

You still have the same problem with the user versus exercise IDs (and some new ones).

Logging from createOneUser():

const createOneExercise = async (userId, newExercise) => {
  try {
    console.log(`my user id is ${userId}`);
    await client.connect();
    newExercise.userId = new ObjectId(userId); // add userId to newExercise object
    const result = await exerciseCollection.insertOne(newExercise);
    console.log('my exercise record:');
    console.log(JSON.stringify(newExercise, null, 2));
    console.log('returning:');
    console.log(JSON.stringify(result, null, 2));
    return result;
  } catch (e) {
    console.error(`Failed to create exercise. ${e}`);
  } finally {
    await client.close();
  }
};

yields

my user id is 641cf8210da2ad5dffb23cfc
my exercise record:
{
  "description": "test",
  "duration": "60",
  "date": "1990-01-01T00:00:00.000Z",
  "userId": "641cf8210da2ad5dffb23cfc",
  "_id": "641cf8220da2ad5dffb23cfd"
}
returning:
{
  "acknowledged": true,
  "insertedId": "641cf8220da2ad5dffb23cfd"
}

You have to return user records with the exercise fields added like in the spec:

{
  username: "fcc_test",
  description: "test",
  duration: 60,
  date: "Mon Jan 01 1990",
  _id: "5fb5853f734231456ccb3b05"
}

Note the types and formats and that the _id field in the spec is what you have labelled as userId. The _id field here is the _id for the user document not the _id for the exercise document. Note that every mongoDB document has an _id field.

1 Like

From what I am understanding, and that I have been getting wrong, is that the exercise document will be assigned some _id but for this exercise the spec doesn’t need that information for the exercise documents. Instead, it needs the user object (username/id) of the id captured from the route parameter, plus the exercise fields (desc/dur./date). And a response of both of those coupled together.

So, the exercise record could have something like this, minus the id field below.

{
  "description": "test",
  "duration": "60",
  "date": "Tue Jan 02 1990",
  "_id": "641ef5b213ebd4db87faa248" // doesn't belong here
}

but that _id shouldn’t be in the response.

And the user object (the route parameter id plus it’s associated name) should be added.

So in the form, instead of putting in an id_ to test for the exercise creation you should be able to simply put in desc./duration and you’ll get the user object + exercise fields added.

So something like this entered into the exercise form:

{
     "description": "exercise-15",
     "duration": 60
}

would produce:

{
  "_id": "641e31110ef8a53de64ae126",
  "username": "jon",
  "description": "exercise-15",
  "duration": 60,
  "date": "Sat Mar 25 2023"
}

with a response object of this:

 res.json({
        _id: userObj._id, // 641e31110ef8a53de64ae126
        username: userObj.username, // jon
        description: newExercise.description,
        duration: newExercise.duration,
        date: newExercise.date,
    });

but that is failing so I must be misunderstanding, at least I think. :slight_smile: This is tough challenge.

I have also updated/changed the code a fair bit in the replit.

Passing all but that test of “user response test /w exercise fields added” whereas last time I was failing 2 or 3. I actually was able to pass the filter test but…this could be wishful thinking that I about got it! :smiley:

Log that last response before returning it and make sure that all the formats, types, and values are correct. duration must be an integer. The date format must be correct. The rest should be strings. The _id must be the user _id.

The reason that the user _id is returned is because that is the only necessary information to connect a user to a record. You can structure your DB as you wish and either store one document per user or store user documents separately from exercise documents. Requiring an exercise _id forces the latter choice.

1 Like

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