Hi Campers,
I am having the issue that my code to get ‘exercise log’ of user failed though I have tried all the condition I can think of. I have compared the output of the sample project and the output of mine and I still can’t figure out what’s the problem with my code.
This is my code so far
app.get('/api/exercise/log', (req, res) => {
let userId = req.query.userId;
let from = req.query.from;
let to = req.query.to;
let limit = req.query.limit;
User.findOne({_id: userId}, {'log._id': 0}, (err, user) => {
if (err) {
console.error(err);
}
if (user) {
let log = user.log.slice();
if (from) {
log = log.filter(exercise => new Date(exercise.date).getTime() >= new Date(from).getTime());
from = new Date(from).toDateString();
}
if (to) {
log = log.filter(exercise => new Date(exercise.date).getTime() <= new Date(to).getTime());
to = new Date(to).toDateString();
}
if (limit >= 1) {
log = log.slice(0, limit);
}
if (from && !to) {
res.json({
'_id': user.id,
'username': user.username,
'from': from,
'count': log.length,
'log': log
});
} else if (to && !from) {
res.json({
'_id': user.id,
'username': user.username,
'to': to,
'count': log.length,
'log': log
});
} else if (from && to) {
res.json({
'_id': user.id,
'username': user.username,
'from': from,
'to': to,
'count': log.length,
'log': log
});
} else {
res.json({
'_id': user.id,
'username': user.username,
'count': log.length,
'log': log
});
}
} else {
res.send('unknown userId');
}
});
});
This is my test output
Only userId:
userId with from condition:
userId with to condition:
userId with from and to condition:
userId with from, to and limit condition:
Hope that I can hear your response for my question soon. Thanks in advance!
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Edg/88.0.705.53
.
Challenge: Exercise Tracker
Link to the challenge: