Hey everyone, I’m having a hard time passing a few of the remaining tests of the exercise tracker.
I pass all other get and post tests and even the first and last GET /api/users/:_id/logs, but not the ones in between. I’ve been trying to recreate it and from what I can tell, I mostly have it, if not, give back the correct properties and values.
Please let me know if you see the problem, feedback is welcome.
const minDate = new Date(-8640000000000000);
const maxDate = new Date(8640000000000000);
app.get("/api/users/:_id/logs", (req, res) => {
const userId = req.params._id;
let fromDate = new Date(req.query.from);
let toDate = new Date(req.query.to);
let logLimit = req.query.limit;
User.findOne({ _id: userId }).select({ "log._id": 0, __v: 0 }).exec((err, userData) => {
if (err) return res.json(err);
if (isNaN(logLimit))
logLimit = userData.log.length;
let filteredLog = userData.log.filter(obj => {
let excDate = new Date(obj.date);
return excDate >= (isNaN(fromDate) ? minDate : fromDate) && excDate <= (isNaN(toDate) ? maxDate : toDate);
}).slice(0, Number(logLimit));
userData.log = filteredLog;
userData.count = userData.log.length;
userData = userData.toObject();
userData.to;
userData.from
if (!isNaN(fromDate))
userData.from = fromDate.toDateString();
if (!isNaN(toDate))
userData.to = toDate.toDateString();
res.json({
_id: userData._id,
username: userData.username,
from: userData.from,
to: userData.to,
count: userData.count,
log: userData.log.slice()
});
});
});