Tell us what’s happening:
Test 9 ( You can make a GET
request to /api/users/:_id/logs
to retrieve a full exercise log of any user.) to Test 15 Fail:
But Test 16 Passes ( 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).
I have the response json returning all the expected fields/values and everything is formatted correctly, as far as I can tell. I currently have it displaying like the example code shows. I’ve also tried looking at other peoples forum posts / examples and tried different methods of searching + filtering, but still can’t get these tests to pass.
I’ve been stuck on this for a few days and really need help!
My Code for the GET request:
app.get('/api/users/:_id/logs', function(req,res){
userModel.findById(req.params._id, (err,user)=>{
if(err) return res.send('Error');
if(!user) return res.send('Unknown User');
exerciseModel.find({user_id:req.params._id},{_id:0,user_id:0,__v:0}, (err,log)=>{
// if 'from' & 'to' query exist, format array.
if(req.query.from && req.query.to){
log = log.filter(e=> new Date(e.date) >= new Date(req.query.from) && new Date(e.date) <= new Date(req.query.to))
}
// if 'limit' query exist, format array.
if(req.query.limit){
log = log.splice(0,req.query.limit)
}
// format date.
log = log.map(e=>{
return ({
description: e.description,
duration: e.duration,
date: new Date(e.date).toDateString()
})
})
res.json({
username: user.username,
count: log.length,
_id: user._id,
log: log
})
})
})
})
Your project link(s)
solution: boilerplate-project-exercisetracker - Node.js Repl - Replit
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36
Challenge: Back End Development and APIs Projects - Exercise Tracker
Link to the challenge: