Not sure where to start with the from/to/limit request. Everything else passes. I’ve looked at many other solutions on the forum and on youtube and can’t understand them and I’ve realized I’ve set mine up differently than all the others I’ve seen- without an exerciseModel. Can anyone help me with how to begin with this last test? Do I need to restructure mine to use an exerciseModel or can I make the log query work with the way I have set mine up?
I opened the snapshot on gitpod and localhost 3000 using my browser. Yes all but sixteen passes, Its seems to be the date handling with Get or the Post Route.
Just in case this helps anyone else one day… this is how I got number 16 to pass. filtering the log array. It got a little messy and may not be the most efficient, but it works.
//Get user object with count and log array when you add /api/users/:_id/logs
app.get('/api/users/:_id/logs', (req, res) => {
let _id = req.params._id;
const from = req.query.from;
const to = req.query.to;
const limit = req.query.limit;
userModel.findById(_id).then((userFound) => {
//if a from or to or limit query exists
if (from || to) {
let filteredLog = userFound.log;
if (from && to) {
filteredLog = filteredLog.filter((exercise) => {
let mathDate = new Date(exercise.date);
return mathDate >= new Date(from) && mathDate <= new Date(to)
})
} else {
if (from) {
filteredLog = filteredLog.filter((exercise) => {
console.log("true or false", new Date(exercise.date) <= new Date(to));
return new Date(exercise.date) >= new Date(from)
})
} else {
if (to) {
filteredLog = filteredLog.filter((exercise) => {
return new Date(exercise.date) <= new Date(to)
})
}
}
};
if (limit) {
filteredLog = filteredLog.slice(0, parseInt(limit))
}
res.json({_id: userFound._id,
username: userFound.username,
count: filteredLog.length,
log: filteredLog
})
}
//if limit query exists, but no from or to
else {
if (limit) {
let limitedLog = userFound.log.slice(0, parseInt(limit))
res.json({_id: userFound._id,
username: userFound.username,
count: limitedLog.length,
log: limitedLog
})
}
//if no queries exist
else {
console.log("You found me", userFound)
res.json(userFound)
}
}
})
})