Tell us what’s happening:
I can’t figure out the desired output format for this test:
I can add an exercise to any user by posting form data userId(_id), description, duration, and optionally date to /api/exercise/add. If no date supplied it will use current date. App will return the user object with the exercise fields added.
It is really confusing what is actually wanted here. I even looked at the source code for the test on github (/curriculum/challenges/english/05-apis-and-microservices/apis-and-microservices-projects/exercise-tracker.english.md), but still can’t seem to get it right for some reason.
Here is example output when I add an exercise:
{"username":"testuser","description":"test description","duration":"60","_id":"5f125d3049ad7b01dcd1f331","date":"Mon Jan 01 1990"}
This looks exactly the same as the expected output in the source code, so I have no idea how to fix this. Also there is no indication in the problem description that the date returned here should be formatted with toDateString()
, which is pure evil as well.
Your code so far
View the full code on Glitch:
module.exports.addExercise = (newExercise, cb) => {
console.log("adding exercise");
const date = newExercise.date ? newExercise.date : new Date();
const insertExercise = {
description: newExercise.description,
duration: newExercise.duration,
date: date
};
User.findOneAndUpdate(
{ _id: newExercise.userId },
{ $push: { exercises: insertExercise } },
{ new: true, useFindAndModify: false },
(err, data) => {
if (err) {
console.log(err);
} else {
const date = new Date(insertExercise.date).toDateString();
const returnData = {
username: data.username,
description: insertExercise.description,
duration: insertExercise.duration,
_id: newExercise.userId,
date: date
}
console.log(returnData)
cb(null, returnData);
}
}
);
};
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.119 Safari/537.36
.
Challenge: Exercise Tracker
Link to the challenge: