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: