What’s happening:
I have no idea why the fourth test is failing. All other tests are passing, but I have spent around 2.5 hours just on this one, and nothing seems to be working. The return format is not specified so I am returning a json object with keys [_id, username, duration, description, date]. Should the return format be different or something?
Thank you.
Code so far
app.post("/api/exercise/add", (req, resp) => {
const { userId, description } = req.body;
let { date, duration } = req.body;
if (!(userId && description && duration)) {
return resp.status(400).json({ msg: "Missing information" });
}
if (date && Number.isNaN(Date.parse(date))) {
return resp.status(401).json({
msg: "'date' value is invalid "
});
}
duration = Number.parseInt(duration);
if (Number.isNaN(duration) || duration <= 0) {
return resp.status(401).json({
msg: "'duration' value is invalid "
});
}
User.findById(userId)
.then(user => {
if (!user) {
return resp
.status(404)
.json({ msg: `User with id ${userId} not found` });
}
if (!date) {
date = new Date();
} else {
date = new Date(date);
}
const exer = new Exercise({
userId,
description,
duration,
date
});
exer
.save()
.then(newExer => {
const finalObj = {
_id: user._id,
username: user.username,
description: newExer.description,
duration: newExer.duration,
date: moment(newExer.date).format("YYYY-MM-DD"),
};
resp.status(200).send(finalObj);
})
.catch(err => {
console.error(err);
resp.status(500).json({ msg: "Internal Error" });
});
})
.catch(err => {
resp.status(500).json({ msg: "Invalid value for userId" });
});
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0
.
Challenge: Exercise Tracker
Link to the challenge: