Tell us what’s happening:
I have been trying to complete the APIs and Microservices Projects - Exercise Tracker.
All are working fine except one-
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 working in the gui present at
`Glitch`` and also using postman.
Your code so far
router.post('/add', (req, res, next) => {
let { userId, description, duration, date } = req.body;
User.findOne({ _id: userId }).then(user => {
if (!user) throw new Error('Unknown user with _id');
date = date || Date.now();
return Exercise.create({
description, duration, date, userId
})
.then(ex => res.status(200).send({
_id: user._id,
username: user.username,
date: moment(ex.date).format('ddd MMMM DD YYYY'),
duration: parseInt(duration),
description: description
}))
})
.catch(err => {
console.log(err);
res.status(500).send(err.message);
})
})
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36
.
Challenge: Exercise Tracker
Link to the challenge: