Tell us what’s happening:
Hi,
I’m stucked on /api/users/:_id/logs
. All tests on that endpoint are false and I have no idea how I can fix them.
You can make a `GET` request to `/api/users/:_id/logs` to retrieve a full exercise log of any user.
... etc...
When I try with my records I get this json :
// https://boilerplate-project-exercisetracker.paulpiazza.repl.co/api/users/63b4226cff12a17f977dff77/logs
{
"_id": "63b4226cff12a17f977dff77",
"username": "paul",
"count": 1,
"log": [
{
"description": "ici teste 1",
"duration": 2,
"date": "Mon Dec 26 2022"
}
]
}
Why my endpoint doesn’t pass the test ?
This is my endpoint :
app.get('/api/users/:_id/logs', (req, res) => {
const idUser = req.params._id
const from = req.query.from || 0
const to = req.query.to || Date.now()
const limit = req.query.limit
User.findById(idUser).then((user) => {
if (!user) res.status(404).json({ error: `No user found with id:${idUser}` })
return Exercise
.find({ username: user.username })
.select({_id: 0, description: 1, duration: 1, date: 1 })
.sort({ date: -1 })
.limit(limit)
.where('date').gte(from).lte(to)
.exec()
.then((exercises) => {
if (!exercises) res.status(404).json({ error: `No exercises found for ${user.username}` })
res.json({
_id: idUser,
username: user.username,
count: exercises.length,
log: exercises
})
})
}).catch(err => res.status(500).json({ error: "Server Error. Please try later." }))
})
I will be happy to have any advice on my code.
Your project link(s)
solution: boilerplate-project-exercisetracker - Replit
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
Challenge: Back End Development and APIs Projects - Exercise Tracker
Link to the challenge: