Exercise Tracker 8th test

Can’t complete the 8th requirement The response returned from POST /api/users/:_id/exercises will be the user object with the exercise fields added.
Don’t really know what’s wrong with it.

let userSchema = new mongoose.Schema({
  username: { type: String, required: true },
  log: [{
    description: { type: String, required: true },
    duration: { type: Number, required: true },
    date: String
  }]
})

app.post("/api/users/:_id/exercises", bodyParser.urlencoded({ extended: false }), (req, res) => {
  let responseObject = {}
  const id = req.body[':_id']
  const description = req.body.description
  const duration = parseInt(req.body.duration)
  let date = req.body.date
  if (date === "")
    date = new Date().toDateString()
  else
    date = new Date(date).toDateString()
  const new_log = {
    description: description,
    duration: duration,
    date: date
  }
  user_Model.findByIdAndUpdate(id, { $push: { log: new_log } }, { new: true }, (err, save) => {
    if (save === undefined || save === null)
      return res.json("Invaild Id")
    else if (err)
      return console.log(err)
    else
      responseObject = {
        username: save.username,
        description: description,
        duration: duration,
        _id: save._id,
        date: date
      }
    res.json(responseObject)
  })
});

solution: https://replit.com/@alisterxavier/boilerplate-project-exercisetracker
project link: https://replit.com/@AlisterXavier/boilerplate-project-exercisetracker#server.js

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0

Challenge: Exercise Tracker

Link to the challenge:

I edited your post to make the links work.

if (date === "")

Are you sure date is an empty string when not supplied? I would log it out.

But i’ve checked the network logs and all of the requests are without any id and so the response would be “Invalid id”.


The other logs are also similar witout any ids

Have a look at this thread.

https://forum.freecodecamp.org/t/please-i-dont-understand-why-my-code-is-not-passing-test-6

Yea… i just changed it to check the date for any undefined values and it worked… The above thread also helped…
Thankies :blush:

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