Hey,
I am working on the BackEnd Certification path and currently stuck with the ExerciseTracker Project.
My code passes all the tests but the last one.
There is no error in the console, it just says Timed Out
I think this code is correct, why is the test failing? Is there any test constraint other than what the result says?
Test Result:
You can add from, to and limit parameters to a GET /api/users/:_id/logs request to retrieve part of the log of any user. from and to are dates in yyyy-mm-dd format. limit is an integer of how many logs to send back. (Test timed out)
My Code:
app.get(’/api/users/:_id/logs’, async(req,res) => {
try{
const {from, to, limit} = req.query;
const id = req.params._id;let myUserDoc = await User.findById(id) let myUser = myUserDoc.toObject(); let theUser = { userId: id } let myExercise = await Exercise.find(theUser) let myLog = [] await myExercise.map(d => myLog.push({ description: d.description, duration: d.duration, date: new Date(d.date.split('T')[0].split('-')[0],d.date.split('T')[0].split('-')[1]-1,d.date.split('T')[0].split('-')[2]).toDateString() }))
if(from || to){
let fromDate = new Date(0)
let toDate = new Date()if(from){ fromDate = new Date(from) } if(to){ toDate = new Date(to) } myLog = myLog.filter((exerciseItem) =>{ let exerciseItemDate = new Date(exerciseItem.date) return ( exerciseItemDate.getTime() >= fromDate.getTime() && exerciseItemDate.getTime() <= toDate.getTime()) })
}
if(limit){ myLog = myLog.slice(0, parseInt(limit)) } res.json({ username: myUser.username, count: myExercise.length, _id: id, log: myLog })
}
catch(err){
console.log(err)
}
})