Back End Development and APIs Projects - Exercise Tracker

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:

Log your route inputs and all your responses (looks like you have the responses logged) from this route and then run the fCC tests against your project to see what the route is doing during testing. Sometimes the problem is in other routes as some tests use multiple routes before testing a final one, so you may need to log the inputs and responses from all your routes to debug the problem.

For more specific help, post a repl so that we can fork and debug your project in a browser.

duration is supposed to be of type Number but I see it as a String in your response.

2 Likes

Nop:

console.log({
  username: typeof user.username,
  description: typeof description,
  duration: typeof duration,
  date: typeof date.toDateString(),
  _id: typeof user._id.toHexString()
})

Result:

{
  username: 'string',
  description: 'string',
  duration: 'number',
  date: 'string',
  _id: 'string'
}

I tried in replit and you’re right, it comes as string.
Thank you so much! I’ve completed all projects. :duck::two_hearts:

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