Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
I want to know why the log array doesn't return the specified user with the given id?
    app.get("/api/users/:_id/logs", (req, res) => {
  const id = req.params._id;
  const { from, to, limit } = req.query;

** Here I tried to search for the matched user and it works successfully: **

  User.findById({ _id: id }, (err, user) => {
    if (!user || err) {
      res.send("Unknown User Id !!");
    } else {

**Then I tried to filter the log array with date **

 // 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;

**try to build the array log and return it to the user but it always be empty and never return the exercises for the user **

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,
              });
            }
          }
        });
    }
  });
});

this is the result when I try to log all the exercises for the user {"username":"ahmed","count":0,"_id":"62a9aab2743ddfc9df5165f2","log":[]}

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda