Why its not passing “add exercise” test case but working properly on glicth…I have tried many ways but still not passing.Please look for it
CODE for /api/exercise/add
exports.add = function(req, res, next) {
let user_id = req.body.userId,
description = req.body.description,
duration = req.body.duration,
date = req.body.date ? new Date(req.body.date) : new Date()
if(req.body.userId) {
User.findOne({_id: user_id}, function(err, data) {
if(err) next(err);
if(!data || data._id !== user_id) {
next({status: 400, message: 'unknown _id'});
} else {
User.findByIdAndUpdate(user_id,
{$push: {log: {description, duration, date}} },
{upsert: true, new: true},
(err, data)=> {
if(err) next(err);
if(!data) { next({status: 400, message: 'unknown _id'}) }
else { res.json({
username: data.username,
_id: data._id,
description,
duration,
date : date.toDateString()
}) }
});
}
});
} else { next({status: 400, message: 'unknown _id' })}
if(!description) {next({status: 400, message: 'missing description'})}
if(!duration) {next({status: 400, message: 'missing duration'})}
}
code :
Link to the challenge: