Tell us what’s happening:
Hi and Happy New Year!
My question concerns the endpoint : GET /api/users/:_id/logs?[from][&to][&limit]
I created a getter
in my Exercise
model.
const exerciseSchema = mg.Schema({
username: { type: String, required: true },
description: { type: String, required: true },
duration: { type: Number, required: true, min: 0 },
date: {
type: Date,
required: true,
get: (date) => {
return new Date(date).toDateString()
}
}
})
In my endpoint, I created a select
//...
app.get('/api/users/:_id/logs', (req, res) => {
// ...
User.findById(idUser).then((user) => {
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}` })
user._doc.log = exercises
user._doc.count = exercises.length
res.json(user)
})
// ...
I receive this json:
{
"_id": "63b2f168fb513f9611f2cfc3",
"username": "le der",
"count": 1
"log": [
{
"description": "ici teste 1",
"duration": 2,
"date": "2022-12-26T00:00:00.000Z" // toDateString() not working
}
],
}
Why the getter
does not work with the find
method? I should fix it with an Array.map
? This not appears to the best solution.
I need help…
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: