Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!