Exercise Tracker last one test error of test time out

app.get("/api/users/:_id/logs", (req, res) => {
  const from = req.query.from;
  const to = req.query.to;
  const limit = req.query.limit;

  let idJson = { "id": req.params._id };
  let idToCheck = idJson.id;

  // Check ID
  UserInfo.findById(idToCheck, (err, data) => {
    var query = {
      username: data.username
    }

    if (from !== undefined && to === undefined) {
      query.date = { $gte: new Date(from) }
    } else if (to !== undefined && from === undefined) {
      query.date = { $lte: new Date(to) }
    } else if (from !== undefined && to !== undefined) {
      query.date = { $gte: new Date(from), $lte: new Date(to) }
    }

    let limitChecker = (limit) => {
      let maxLimit = 100;
      if (limit) {
        return limit;
      } else {
        return maxLimit
      }
    }

    if (err) {
      console.log("error with ID=> ", err)
    } else {

      ExerciseInfo.find((query), null, { limit: limitChecker(+limit) }, (err, docs) => {
        let loggedArray = [];
        if (err) {
          console.log("error with query=> ", err);
        } else {

          let documents = docs;
          let loggedArray = documents.map((item) => {
            return {
              "description": item.description,
              "duration": item.duration,
              "date": item.date.toDateString()
            }
          })

          const test = new LogInfo({
            "username": data.username,
            "count": loggedArray.length,
            "log": loggedArray,
          })

          test.save((err, data) => {
            if (err) {
              console.log("error saving exercise=> ", err)
            } else {
              console.log("saved exercise successfully");
              if (!from) {
                res.json({
                  "_id": idToCheck,
                  "username": data.username,
                  "count": data.count,
                  "log": loggedArray
                })
              }
              else {
                res.json({
                  "_id": idToCheck,
                  "username": data.username,
                  "from": new Date(req.query.from).toDateString(),
                  "to": new Date(req.query.to).toDateString(),
                  "count": data.count,
                  "log": loggedArray
                })
              }
            }
          })
        }
      })
    }
  })
})

“Error” => You can add

from

,

to

and

limit

parameters to a

GET /api/users/:_id/logs

request to retrieve part of the log of any user.

from

and

to

are dates in

yyyy-mm-dd

format.

limit

is an integer of how many logs to send back. (Test timed out)

Hello there.

Do you have a question?

If so, please edit your post to include it.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more information you give us, the more likely we are to be able to help.

Hi @JeremyLT , I am having the same issue as the person who created this post.

He was asking why the final test is failing which is this test =>

You can add from , to and limit parameters to a GET /api/users/:_id/logs request to retrieve part of the log of any user. from and to are dates in yyyy-mm-dd format. limit is an integer of how many logs to send back.

I have a similar conditional to him for the to, from and limit parameters and mine does not work either, I have tried to change the conditional and in doing this other tests fail. So with the conditional as is, every other test passes except the one specified above.

Any help with this will be greatly appreciated.