HI.
I’m doing the Exercise Tracker challenge. All tests pass successfully, except the eighth, the one that says:
The response returned from
POST /api/users/:_id/exercises
will be the user object with the exercise fields added.
It seems to me that this is precisely the behavior of the application: the response of the example application is
{
"_id": "61204ee9f5860e05a3652f11",
"username": "fcc_test_16295073016",
"date": "Wed Jul 03 2019",
"duration": 42,
"description": "test for post"
}
while my application’s response is
{
"username": "myself",
"description": "first test for posting data",
"duration": 42,
"date": "Wed Jul 03 2019",
"_id": "66c2224d1a07019654f170e8"
}
Below is the query that is called by my application:
const createAndSaveExercise = (exercise, done) => {
const id = exercise._id;
// search for an existing id
models.userModel
.findById(id)
.then((doc) => {
if (doc == null) {
// if it doesn't find it does nothing
done(null, null);
} else {
// else create and save an exercise for that id
let { _id, description, duration, date } = exercise;
let username = doc.username;
date = new Date(date).toDateString();
if (String(date).toLowerCase() == "invalid date") {
date = new Date(Date.now()).toDateString();
}
let newExercise = new models.exerciseModel();
newExercise.userId = _id;
newExercise.description = description;
newExercise.duration = duration;
newExercise.date = date;
newExercise
.save()
.then((doc) => {
doc.username = username;
done(null, doc);
})
.catch((err) => {
done(err);
});
}
})
.catch((err) => {
done(err);
});
};
and then the function that manages the routing:
app.post("/api/users/:_id/exercises", urlEncodedParser, (req, res) => {
const exercise = {
_id: req.params._id,
description: req.body.description,
duration: req.body.duration,
date: req.body.date,
};
dbmanager.createAndSaveExercise(exercise, (err, data) => {
if (err) {
res.json({
error: `error creating a new exercise: "${err}"`,
});
} else {
if (data) {
const { username, description, duration, date, _id } = data;
res.json({
username,
description,
duration,
date: new Date(date).toDateString(),
_id,
});
} else {
res.json({
error: "there isn't any user with the supplied id",
});
}
}
});
});
What could be the problem?