Hi there,
I’ve been stuck here for hours and I can’t find they to solve it. Although it does what it is supposed to do, I can’t complete the last challenge. I thought it could be something related with the Date objects but to be honest I don’t know because it is working properly.
Here is my code:
app.get('/api/exercise/log', (req,res) => {
if(req.query.userId) {
const userId = req.query.userId
const user = log.find( user => user._id == userId)
if(req.query.from && req.query.to) {
const fromDate = new Date(req.query.from)
const toDate = new Date(req.query.to)
const limit = req.query.limit && parseInt(req.query.limit)
const result = user.log.filter( ex => new Date(ex.date) >= fromDate && new Date(ex.date) <= toDate)
const resultToLog = limit ? result.slice(0,limit) : result
const toLog = {
...user,
from: fromDate.toDateString(),
to: toDate.toDateString(),
count: resultToLog.length,
log: resultToLog
}
res.json(toLog)
} else {
if(!user) {
res.send('There is no an user with that Id')
} else {
res.json(user)
}
}
} else {
res.send('You need to provide an userId')
}
})
Query to:
https://boilerplate-project-exercisetracker.inked.repl.co/api/exercise/log?userId=3184b0f2-8094-4774-a8c1-38cde50cb268
Response:
{"_id":"3184b0f2-8094-4774-a8c1-38cde50cb268","username":"inked","count":3,"log":[{"description":"abs","duration":34,"date":"2020-02-23T00:00:00.000Z"},{"description":"abs","duration":34,"date":"2020-12-05T00:00:00.000Z"},{"description":"abs","duration":34,"date":"2021-02-11T21:21:07.930Z"}]}
Query to:
https://boilerplate-project-exercisetracker.inked.repl.co/api/exercise/log?userId=3184b0f2-8094-4774-a8c1-38cde50cb268&from=1900-10-05&to=2021-02-28
Response:
{"_id":"3184b0f2-8094-4774-a8c1-38cde50cb268","username":"inked","count":3,"log":[{"description":"abs","duration":34,"date":"2020-02-23T00:00:00.000Z"},{"description":"abs","duration":34,"date":"2020-12-05T00:00:00.000Z"},{"description":"abs","duration":34,"date":"2021-02-11T21:21:07.930Z"}],"from":"Fri Oct 05 1900","to":"Sun Feb 28 2021"}
Query to:
https://boilerplate-project-exercisetracker.inked.repl.co/api/exercise/log?userId=3184b0f2-8094-4774-a8c1-38cde50cb268&from=1900-10-05&to=2021-02-28&limit=1
Response:
{"_id":"3184b0f2-8094-4774-a8c1-38cde50cb268","username":"inked","count":1,"log":[{"description":"abs","duration":34,"date":"2020-02-23T00:00:00.000Z"}],"from":"Fri Oct 05 1900","to":"Sun Feb 28 2021"}
Query to:
https://boilerplate-project-exercisetracker.inked.repl.co/api/exercise/log?userId=3184b0f2-8094-4774-a8c1-38cde50cb268&from=2021-02-05&to=2021-03-01
Response:
{"_id":"3184b0f2-8094-4774-a8c1-38cde50cb268","username":"inked","count":1,"log":[{"description":"abs","duration":34,"date":"2021-02-11T21:21:07.930Z"}],"from":"Fri Feb 05 2021","to":"Mon Mar 01 2021"}