Exercise Tracker - Failed to pass the test but everything works well

It’s happening again
I’m very confused because my projects works very well. But there are two left test that I didn’t pass.

The test I didn’t pass:

The date property of any object in the log array that is returned from GET /api/users/:id/logs should be a string. Use the dateString format of the Date API

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)

Which I believe this happened on GET /api/users/:id/logs part.
This is my code for it (or you can visit my replit for full code, link below):

// Find recordd by id
const findRecordById = (id) => {
  return new Promise(async(resolve, reject) => {
    let getRecord = await User.findOne({_id: id});
    
    if (getRecord !== null) {
      resolve(getRecord);
    } else {
      reject();
    }
  })
}

app.get('/api/users/:_id/logs?', async(req, res) => {
  // Get id
  const id = req.params._id;
  // Get from date as start
  const from = req.query.from;
  // Get to date as end
  const to = req.query.to;
  // Get limit to limit number of exercise
  const limit = req.query.limit;

  // Find user record by id
  // If succed, then do with result
  // Otherwise, catch invalid ID
  await findRecordById(id)
    .then(async(result) => {
      // Find user log exercise by id
      let userExerciseData = await Exercise.findOne({_id: id}, {log: {_id: 0}});

      // If no exercise added yet, send empty log
      if (userExerciseData === null) {
        res.send({
          _id: result._id,
          username: result.username,
          count: 0,
          log: []
        });
      } else {
        // Get only log object
        let logData = userExerciseData.log;

        // If there's from OR to query in user GET
        if (from || to) {
            // Set default from date, as very long old date
            let fromDate = new Date(0);
            // Set default to date, as current date
            let toDate = new Date();

            // If there's from query, set date as it value
            // Otherwise, it will be just default value above
            if (from) {
              fromDate = new Date(from);
            }

            // If there's to query, set date as it value
            // Otherwise, it will be just default value above
            if (to) {
              toDate = new Date(to);
            }

            // Get time of both date as comparison
            fromDate = fromDate.getTime();
            toDate = toDate.getTime();

            // Filter the log by date
            logData = userExerciseData.log.filter((item) => {
              let sessionDate = item.date.getTime();
              return sessionDate >= fromDate && sessionDate <= toDate;
            })
        }

        // Limit the log
        if (limit) {
          logData = logData.slice(0, limit);
        }

        res.send({
          _id: result._id,
          username: result.username,
          count: logData.length,
          log: logData.map((item) => {
            return {
              description: item.description,
              duration: item.duration,
              date: item.date.toDateString()
            }
          })
        })
      }
    }).catch(() => {
      res.end("Invalid ID");
    })
})

First Failed Test: It says returned date should be string. But as you can see, I convert the Date to string in res.send(). Because in my mongodb it store as Date. But it didn’t pass the test.

Second Failed Test: I’ve already add from, to and limit to retrieve part of the log. And it works well with code you see above. But again, it didn’t pass.

I hope you guys know the solution. Thanks.

My project link(s)

Project: boilerplate-project-exercisetracker - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Back End Development and APIs Projects - Exercise Tracker

Link to the challenge:

It just seems unreliable. If you submit a few times it should pass at some point, it did for me anyway.

Not sure if it’s Replit or fCC that is the problem.

1 Like

Still didn’t pass those two test. I have submit multiple times.

I don’t get it, what exactly they want

Try using CodeSandbox for the submit.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.