Hey everyone, I’m almost finished with the exercise tracker! I am able to pass all of the tests for this project except for the 4th one.
I can add an exercise to any user by posting form data userId(_id), description, duration, and optionally date to /api/exercise/add. If no date supplied it will use current date. App will return the user object with the exercise fields added.
While I have seen other users have failed this test due to using userId instead of _id or forgetting to format the date, I’m not sure what is causing me to fail this test. I have seen my app seems to return an object formatted in the exact same way as the demo app.
{"_id":"5f4255a283c84000a74c15ee",
"username":"hobo22",
"date":"Fri Aug 14 2020",
"duration":100,
"description":"hoooo heeyyy"
}
Here is my API for the add exercise:
app.post("/api/exercise/add", async function(req, res) {
const { userId, description, duration, date } = req.body;
let exerciseDate;
if (date) {
exerciseDate = moment(date).format("ddd MMM D YYYY");
} else {
exerciseDate = moment().format("ddd MMM D YYYY");
}
console.log("exerciseDate", exerciseDate, typeof exerciseDate);
const userArray = await user.find({ _id: userId });
const userObject = userArray[0];
const exercise = {
date: exerciseDate,
description: description,
duration: parseInt(duration)
};
const exerciseArray = userObject.exercise_logs;
exerciseArray.push(exercise);
user.findOneAndUpdate(
{ _id: userId },
{ exercise_logs: exerciseArray },
{
new: true
},
(err, data) => {
if (err) {
console.log(err);
}
res.json({
_id: userId,
username: userObject.username,
date: exercise.date,
duration: exercise.duration,
description: exercise.description
});
}
);
});
Here is a link to my project. I appreciate any suggestions you may have! Thanks a lot for all your help over the years!