I am going through a Exercise Tracker challenge and I am not able to pass the last task.
However I added the required parameters and able to retrieve part of the log using them.
All the other task done and accepted so I assume problem somewhere in this last part.
Would appreciate any help, Thank you!
My code
app.get("/api/users/:_id/logs?",async function (req,res){
let typedId = req.params._id
const {from,to,limit} = req.query
let log = [];
try{
let foundLog = await User.findOne({_id:typedId})
if(foundLog){
let foundExeLogs = await Exe.find({key:foundLog._id},{ _id: 0 ,key:0,__v: 0}).lean().exec();
logs = [...foundExeLogs]
// CODE FOR THE LAST TASK BELOW
if(from && to){
let newLogs = await logs.filter(foundLog =>
new Date(foundLog.date).getTime() >= new Date(from).getTime() &&
new Date(foundLog.date).getTime() <= new Date(to).getTime()
)
if(limit){
newLogs = newLogs.slice(0,limit)
}
res.json({
_id:foundLog._id,
username:foundLog.username,
from: new Date(from).toDateString(),
to: new Date(to).toDateString(),
count:newLogs.length,
log:newLogs
})
}else{
// CODE FOR THE LAST TASK ABOVE
res.json({
_id:foundLog._id,
username:foundLog.username,
count:logs.length,
log:logs
})
}
}
}catch (err) {
console.error(err)
res.status(500).json("Server Error")
}
})