app.get("/api/users/:_id/logs", (req, res) => { const id = req.params._id; const { from, to, limit } = req.query;** Aquí traté de buscar el usuario coincidente y funciona con éxito: **
User.findById({ _id: id }, (err, user) => { if (!user || err) { res.send("Unknown User Id !!"); } else {** Luego traté de filtrar la matriz de registro con la fecha **
// const username = user.username; let responObject = {}; if (from) { responObject["$gte"] = new Date(from).toDateString(); } if (to) { responObject["$lte"] = new Date(to).toDateString(); } let filter = { _id: id, }; if (from || to) { filter.date = responObject; } let nonNullLimit = limit ?? 500;** intente construir el registro de matriz y devolvérselo al usuario, pero siempre estará vacío y nunca devolverá los ejercicios para el usuario **
Exercise.find(filter) .limit(+nonNullLimit) .exec((err, data) => { if (err || !data) { res.json([]); } else { const count = data.length; const rowLog = data; const { username, _id } = user; const log = rowLog.map((item) => ({ description: item.description, duration: item.duration, date: new Date(item.date).toDateString(), })); console.log(log) if (from && to) { res.json({ username, from: new Date(from).toDateString(), to: new Date(to).toDateString(), count, _id, log, }); } else { res.json({ username, count, _id, log, }); } } }); } }); });este es el resultado cuando trato de registrar todos los ejercicios para el usuario {"username":"ahmed","count":0,"_id":"62a9aab2743ddfc9df5165f2","log":[]}