Tell us what’s happening:
I’m having troubles with 8th test:
“The response returned from POST /api/users/:_id/exercises will be the user object with the exercise fields added.”
I logged the response before submitting it and it looks like the response expected in the project:
Body:
{
"description": "solving response",
"duration": 1,
"date": "2020-10-10"
}
Log:
{
username: 'admin',
description: 'solving response',
duration: 1,
date: 'Sat Oct 10 2020',
_id: '639f66effd52ac8ed536be84'
}
Response:
{
"username": "admin",
"description": "solving response",
"duration": 1,
"date": "Sat Oct 10 2020",
"_id": "639f66effd52ac8ed536be84"
}
Expected:
{
username: "fcc_test",
description: "test",
duration: 60,
date: "Mon Jan 01 1990",
_id: "5fb5853f734231456ccb3b05"
}
Your code so far
Repo: GitHub - im-victor-mendez/freeCodeCamp-Exercise_Tracker
File direction: routes/user.routes.js - Line 40
router. Post('/:_id/exercises', async (req, res) => {
const _id = req.params._id || req.body[':_id']
const user = await User.findById(_id)
const { description, duration } = req.body
if (!_id || !description || !duration || !user) {
if (!user) res.json({ 'error': 'User not founded' })
console.error('Exercise validation to save has been failed')
res.json({ 'error': 'Exercise validation to save has been failed' })
} else {
let { date } = req.body
if (!date) date = new Date(); else {
date = date.split(/[-./]/)
date = new Date(date[0], date[1] - 1, date[2])
}
const exercise = new Exercise({
username: user.username,
description,
duration,
date,
userId: _id
})
try {
exercise.save()
console.log('Exercise saved')
//To fix response IDK :/
/**
* The response returned from POST /api/users/:_id/exercises will be the user object with
* the exercise fields added.
*/
console.log({
username: user.username,
description,
duration,
date: date.toDateString(),
_id: user._id.toHexString()
})
res.json({
username: user.username,
description,
duration,
date: date.toDateString(),
_id: user._id.toHexString()
})
} catch {
console.error('Error saving exercise, please try again')
console.log(user._id.toHexString())
res.json({ 'error': 'Error saving exercise, please try again' })
}
}
})
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.46
Challenge: Back End Development and APIs Projects - Exercise Tracker
Link to the challenge: