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

Im stuck at problem 8: “The response returned from POST /api/users/:_id/exercises will be the user object with the exercise fields added.”

I’ve seen in various topics what might be causing this issue. I’ve tried everything and nothing works. Many solutions indicate that the return value of _id should be of type ObjectId, so I changed it, but it still didn’t work.

This is my “/api/users/:_id/exercises” code:

app.post(“/api/users/:_id/exercises”, async (req, res) => {
try {
// Encontrar o usuário pelo ID
const user = await User.findById(req.params._id);
if (!user) return res.json({ error: “user doesn’t exist” });

// Criar um novo exercício
const newExercise = await Exercise.create({
  username: user.username,
  description: req.body.description,
  duration: req.body.duration,
  date: req.body.date ? new Date(req.body.date) : new Date(),
});

// Responder com o objeto do usuário incluindo o novo exercício
return res.json({
  username: user.username,
  description: newExercise.description,
  duration: newExercise.duration,
  date: newExercise.date.toDateString(),
  _id: user._id, // Mantém o ID como ObjectId
});

} catch (error) {
console.error(error);
return res.json({ error: “Operation failed” });
}
});

Please, somebody, save me!

Please provide a repo with all your code, or use Replit, Glitch, Gitpod (for Gitpod you have to share a snapshot).


Is your duration of type Number?

Open the browser dev tools and look at the request/response when you submit. Check your response against the example response.

Here it is:

About duration type, I can’t manage to find this request/response thing, but maybe this part of dev tools indicates that is a number type:

It’s the network tab in the dev tools you need to use.


Your code was passing for me with my own DB when I tested it.

  1. Try deleting your collection on Atlas.
  2. Try using Gitpod, Replit, or Glitch. Sometimes people have issues with the date when running locally.

Owwww Yeah!!! It Worked!!!

To elucidate what happened:
-I tried deleting, but it didn’t work
-I tried use Gitpod and Glitch, but they didn’t work either
-Finally, it worked on Replit!

Thank you very much!!!