It works fine when I try it out on the db here is the format of json response I am getting. The last test case is still failing, what could be the reason?
Link for project: https://replit.com/@PushkarMahajan/boilerplate-project-exercisetracker-5#server.js
{"_id":"61270a9fc3c23da04e8f9c68","username":"fcc_test_16299485731","from":"Mon Dec 31 1990","to":"Wed Jan 03 1990","count":2,"log":[{"description":"test","duration":60,"date":"Mon Jan 01 1990"},{"description":"test","duration":60,"date":"Tue Jan 02 1990"}]}
app.get("/api/users/:_id/logs", (req, res) => {
const {from , to } = req.query;
if(from == undefined)
{
UserModel.find({ _id: req.params._id}, (err, found) => {
let tempLog = [];
found[0].log.map((element) => {
tempLog.push({
description: element.description,
duration: element.duration,
date: element.date.toDateString()
});
})
res.json({
_id: req.params._id,
username: found[0].username,
count: parseFloat(found[0].log.length),
log: tempLog
});
})
}
else{
let tempDate = [];
console.log(req.query);
UserModel.findOne({ _id: req.params._id}, (err, found) => {
let resultLog = found.log;
if(req.query.from)
{
const fromDate = new Date(from);
resultLog=found.log.filter((exec) => exec.date >= fromDate)
}
if(req.query.to)
{
const toDate = new Date(to);
resultLog = found.log.filter((exec) => exec.date <= toDate)
}
if(req.query.limit)
{
resultLog=resultLog.splice(0, req.query.limit)
}
let finalLog = [];
resultLog.map((ele) => {
finalLog.push({
description: ele.description,
duration: ele.duration,
date: ele.date.toDateString()
});
})
const fromDate = new Date(from).toDateString();
const toDate = new Date(to).toDateString();
res.json({
_id: req.params._id,
username: found.username,
from: fromDate,
to: toDate,
count: parseFloat(finalLog.length),
log: finalLog
})
})
}
})