** You can add from
, to
and limit
parameters to a GET /api/users/:_id/logs
request to retrieve part of the log of any user. from
and to
are dates in yyyy-mm-dd
format. limit
is an integer of how many logs to send back.
all looks ok working on browser fine but not pass this task why i dont know
my code
// Get All Log
app.get(’/api/users/:_id/logs?’, async (req, res) => {
try{
console.log(req.params);
console.log(req.query);
let user = await Users.findById(req.params._id).exec();
let exercise = await Exercises.find({user_id : req.params._id})
.select(’-_id -__v -user_id’)
.exec();
if(typeof user.username !== 'undefined' && exercise.length > 0){
//var from_dt = new Date(req.query.from);
//var to_dt = new Date(req.query.to);
// if(from_dt.toDateString() !== 'Invalid Date' && to_dt.toDateString() !== 'Invalid Date'){
// exercise = exercise.filter(a => {
// var date = new Date(a.date);
// return (date >= from_dt && date <= to_dt);
// });
// }
//exercise = exercise.slice(0, parseInt(req.query.limit) ? parseInt(req.query.limit) : exercise.length);
if((req.query.from) && (req.query.to)){
exercise = exercise.filter((d)=> (Date.parse(d.date) >= Date.parse(req.query.from)) && (Date.parse(d.date) <= Date.parse(req.query.to)));
}
//check if index is smaller then LIMIT
if(req.query.limit){
let limit = parseInt(req.query.limit)
exercise = exercise.filter((d,i)=> i < limit);
}
var data = {
username: user.username,
count: parseInt(exercise.length),
_id: user._id
}
for(i = 0; i < exercise.length; i++ ){
var date_obj = new Date(exercise[i]['date']);
let description = exercise[i]['description'];
let duration = exercise[i]['duration'];
exercise[i] = {};
exercise[i]['description'] = description;
exercise[i]['duration'] = parseInt(duration);
exercise[i]['date'] = date_obj.toDateString();
}
data['log'] = exercise;
if(exercise.length > 0){
res.json(data);
}else{
res.json({error:'User exercise found'});
}
}else{
res.json({error:'User Id not found'});
}
}catch(err){
console.log(err)
}
});
My project link(s)
solution: boilerplate-project-exercisetracker-1 - Replit
browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36
Challenge: Exercise Tracker
Link to the challenge: