I’ve just finished my exercise tracker project and everything works fine but the problem is that most of the time I get “test timed out” on filtering the exercise log
So my main questions are:
- Is it because my way of filtering is too slow?
- If so, what is a better way to do the filtering?
Source code: https://glitch.com/edit/#!/exercise-tracker-moon
Demo: https://exercise-tracker-moon.glitch.me/
This is my code on logging and filtering:
router.get("/log", async (req, res) => {
try {
const {userId, from, to, limit} = req.query;
const user = await User.findById(userId);
if(user) {
var resObj = {
_id: user._id,
username: user.username
};
var log = await Exercise.find({userId})
.select('-_id description duration date')
.exec();
//Filter...
if(from) {
let fromDate = new Date(from);
log = log.filter(each => each.date.getTime() >= fromDate.getTime())
resObj.from = fromDate.toDateString();
}
if(to) {
let toDate = new Date(to);
log = log.filter(each => each.date.getTime() <= toDate.getTime());
resObj.to = toDate.toDateString();
}
if(limit) {
log = log.slice(0, limit)
}
//Clean log data
log = log.map(eachObj => {
return {
description: eachObj.description,
duration: eachObj.duration,
date: eachObj.date.toDateString()
}
})
resObj.count = log.length;
resObj.log = log;
console.log("\n\n", resObj);
return res.json(resObj);
}
throw Error("Unknown UserId!")
}
catch(err) {
console.log("error...", err.message)
return res.json({'error': err.message});
}
})
Link to the Challenge: FCC Exercise Tracker Project