All my tests are OK except #4:
You can POST to /api/users/:_id/exercises with form data ‘description’, ‘duration’, and optionally ‘date’. If no date is supplied, the current date will be used. The response returned will be the user object with the exercise fields added.
(URL in index.html and tests are different)
I have following code (truncated):
const userSchema = mongoose.Schema(
{
username: { type: String, required: true, unique: false },
exercices: [
{
description: { type: String },
duration: { type: Number },
date: { type: String, required: false }
}
]
}
);
const User = mongoose.model('Users', userSchema);
const defaultDate = () => new Date().toISOString().slice(0, 10);
function addExercise(req, res) {
const userId = req.params.userId || req.body.userId; // userId come from URL or body
const exObj = {
description: req.body.description,
duration: +req.body.duration,
date: req.body.date || defaultDate()
};
User.findByIdAndUpdate(
userId,
{$push: { exercices: exObj } },
{new: true},
function (err, updatedUser) {
if(err) {
return console.log('update error:',err);
}
let returnObj = {
_id: updatedUser.id,
username: updatedUser.username,
description: exObj.description,
duration: exObj.duration,
date: exObj.date
};
res.json(returnObj);
}
);
}
app.all("/api/users/:userId/exercises", addExercise);
debug show this:
- POST to URL /api/users/607d75bee17ce700fff750fe/exercises
- BODY: { description: ‘test’, duration: ‘60’, date: ‘1990-01-01’}
Object to return:
{
_id: '607d75bee17ce700fff750fe',
username: 'fcc_test_16188348755',
description: 'test',
duration: 60,
date: '1990-01-01'
}
Test fails.
I can’t figure what is wrong.