I’ve been working on the exercise tracker microservice project for the past several days and can’t seem to pass the “The response returned from POST /api/users/:_id/exercises
will be the user object with the exercise fields added” test.
Looking at the code for the test, I did my best to troubleshoot by building the deepEqual
assertion into the route itself. So my route is currently written as follows:
// create an exercise for a user
app.post('/api/users/:_id/exercises', async function(req, res) {
console.log("In the exercises/workouts POST request");
// the body looks like this:
// {":_id":"66c4fac95d65b665ec1e721b","description":"running","duration":"50","date":"2024-08-20"}
const user_id = req.params._id;
const description = req.body.description;
const duration = req.body.duration;
const date = req.body.date;
console.log(`${user_id}, ${description}, ${duration}, ${date}`)
try {
const user = await User.findById(user_id);
if (!user) {
res.send("Could not find user.")
} else {
const workoutDocument = new Workout({
user_id: user._id,
description: description,
duration: duration,
// if no date is provided, use the current date
date: date ? new Date(date).toDateString() : new Date().toDateString()
});
try {
// save the new workout to the database
const workout = await workoutDocument.save();
console.log(JSON.stringify(workout.date.toDateString()), typeof(JSON.stringify(workout.date.toDateString())))
console.log(`
${user.username, typeof(user.username)},
${workout.description, typeof(workout.description)},
${workout.duration, typeof(workout.duration)},
${workout.date.toDateString(), typeof(workout.date)},
${workout._id, typeof(workout.user_id)},
`)
console.log(deepEqual(JSON.stringify(workout), JSON.stringify({
username: user.username,
usernameType: typeof(user.username),
description: workout.description,
descriptionType: typeof(workout.description),
duration: Number(workout.duration),
durationType: typeof(workout.duration),
date: workout.date.toDateString(),
dateType: typeof(workout.date.toDateString()),
_id: workout._id,
_id: workout.user_id
})))
res.json({
username: user.username,
usernameType: typeof(user.username),
description: workout.description,
descriptionType: typeof(workout.description),
duration: Number(workout.duration),
durationType: typeof(workout.duration),
date: workout.date.toDateString(),
dateType: typeof(workout.date.toDateString()),
_id: workout._id,
_id: workout.user_id
})
} catch (err) {
console.log(err);
res.send(`There was an error with saving the workout: ${err}`);
}
}
} catch (err) {
console.log(err);
res.send(`There was an error with retrieving the user: ${err}`);
}
});
When I run the project locally and try to add an exercise in the UI using the route as coded above, I get the following error:
AssertionError [ERR_ASSERTION]: Expected values to be loosely deep-equal:
'{"user_id":"66cf57b82fb30bf35d5e46a1","description":"running","duration":20,"date":"2024-08-28T04:00:00.000Z","_id":"66cf95835c60fac6dc377d6c","__v":0}'
should loosely deep-equal
'{"username":"dylanc","usernameType":"string","description":"running","descriptionType":"string","duration":20,"durationType":"number","date":"Wed Aug 28 2024","dateType":"string","_id":"66cf57b82fb30bf35d5e46a1"}'
at /Users/mgermaine93/Desktop/CODE/fcc-code-challenges/back-end-development-and-apis/projects/exercise-tracker-microservice/index.js:144:21
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: '{"user_id":"66cf57b82fb30bf35d5e46a1","description":"running","duration":20,"date":"2024-08-28T04:00:00.000Z","_id":"66cf95835c60fac6dc377d6c","__v":0}',
expected: '{"username":"dylanc","usernameType":"string","description":"running","descriptionType":"string","duration":20,"durationType":"number","date":"Wed Aug 28 2024","dateType":"string","_id":"66cf57b82fb30bf35d5e46a1"}',
operator: 'deepEqual'
}
It looks like the actual
and expected
values don’t quite match up, particularly with the date
, user_id
, and _id
values. Ignoring removing the ...Type
keys and values from my expected
output, I’m not sure how to fix this. Any ideas?
All of the other tests are passing. I’m running this locally. The code for my entire exercise tracker project is here.
Thank you in advance.