Exercise tracker: last test failing

the last test is failing which requires us to be able to send queries like from,to and limit.

my responses are as below for the logs endpoint:

w/ the queries

w/o the queries
Uploading: Screenshot (509).png…

:
please find the code here: replit

this is the failing 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 am currently accessing the from,to and limit from the query attr. of the req object like req.query.from ?

I am trying to following this endpoint structure too, given in the html
GET user’s exercise log: GET /api/users/:_id/logs?[from][&to][&limit]

Log your route inputs and outputs and run the fCC tests and then look at the console on repl.it and you’ll see a bunch of errors because userExercises is undefined. You need to check your conditional in which you set userExercises because there is currently no output for these tests to check.

app.get('/api/users/:id/logs?', async (req, res) => {
  // Log inputs.
  console.log(`req.body: ${JSON.stringify(req.body)}`);
  console.log(`req.params: ${JSON.stringify(req.params)}`);
  console.log(`req.query: ${JSON.stringify(req.query)}`);

  const { from, to, limit } = req.query;
  let queryLimit = Number(limit)
  let count;
  let logsArray = [];
  let userExercises;


  let user = await UserModel.findById({ _id: req.params.id })
  let { username } = user;

  // This conditional is part of the problem.
  // It's only true if there are no queries or there are all queries.
  if (Object.keys(req.query) === 0) {
    userExercises = await ExerciseModel.find({ username })
      .select({ _id: false, username: false, __v: false })
      .exec();

    count = userExercises.length
  }

  else {
    if (!isNaN(new Date(from)) && !isNaN(new Date(to)) && limit) {
      console.log('hue')
      userExercises = await ExerciseModel.find({ username })
        .find({ "date": { $gte: new Date(from).toDateString(), $lt: new Date(to).toDateString() } })
        .select({ _id: false, username: false, __v: false })
        .limit(queryLimit)
        .exec();

      console.log('Userxercise--->', userExercises)

      count = userExercises.length
    }
  }

  // This is undefined!
  console.log(userExercises);

  // This fails (look at the console errors since `userExercises` is undefined.
  // Execution stops here, hence the timeout errors on the tests.
  userExercises.map((item) => {
    console.log(item)
    logsArray.push(item)
  })

  //save logs
  let savedLogs = await new LogsModel({
    username,

    count,
    log: userExercises
  }).save();

  // Log outputs.
  console.log({
    username,
    _id: user._id,
    count,
    log: logsArray
  });
  // Return responses.
  return res.status(200).json({
    username,
    _id: user._id,
    count,
    log: logsArray
  })
})
1 Like

Thank you so much my man @jeremy.a.gray <3

Yes, I spotted the fist gotcha in the conditional : where I was testing if the query obj is empty

so i added Object.keys(req.query).length === 0 which basically checks if the query params is empty by extracting the keys into an array and checking if the array length is zero.

update: I am getting userExercises in either cases i.e, with and without the queries:

yet the final test is failing… :frowning:
Welp!!

edit: I found where the test was failing, the test failed when the reqs are made w/ “from” and “to” query params and not limit.
seems like I have to handle this use case too.

This is the failing test and console error:

req.params: {“id”:“61a711358db097b9ff990b0e”}
req.query: {“from”:“1989-12-31”,“to”:“1990-01-03”}
hue
(node:1437) UnhandledPromiseRejectionWarning: MongoServerError: Limit value must be non-negative, but received: -9223372036854775808

EDIT:

I could remove the above error, I was mistakenly passing a limit variable where it didn’t exist.

Well, then I could see all my console messages while FCC tests my project, I saw no error, yet the final test failed :////