Can't pass test 5 and 6 of APIs and Microservices Projects - Exercise Tracker

Hi Campers,
I am having the issue that my code to get ‘exercise log’ of user failed though I have tried all the condition I can think of. I have compared the output of the sample project and the output of mine and I still can’t figure out what’s the problem with my code.

This is my code so far

app.get('/api/exercise/log', (req, res) => {
  let userId = req.query.userId;
  let from = req.query.from;
  let to = req.query.to;
  let limit = req.query.limit;
  User.findOne({_id: userId}, {'log._id': 0}, (err, user) => {
      if (err) {
        console.error(err);
      }
      
      if (user) {
        let log = user.log.slice();

        if (from) {
          log = log.filter(exercise => new Date(exercise.date).getTime() >= new Date(from).getTime());
          from = new Date(from).toDateString();
        } 
        if (to) {
          log = log.filter(exercise => new Date(exercise.date).getTime() <= new Date(to).getTime());
          to = new Date(to).toDateString();
        }
        if (limit >= 1) {
          log = log.slice(0, limit);
        }

        if (from && !to) {
          res.json({
            '_id': user.id,
            'username': user.username,
            'from': from,
            'count': log.length,
            'log': log
          });
        } else if (to && !from) {
          res.json({
            '_id': user.id,
            'username': user.username,
            'to': to,
            'count': log.length,
            'log': log
          });
        } else if (from && to) {
          res.json({
            '_id': user.id,
            'username': user.username,
            'from': from,
            'to': to,
            'count': log.length,
            'log': log
          });
        } else {
          res.json({
            '_id': user.id,
            'username': user.username,
            'count': log.length,
            'log': log
          });
        }
      } else {
        res.send('unknown userId');
      }
    });       
});

This is my test output
Only userId:


userId with from condition:

userId with to condition:

userId with from and to condition:

userId with from, to and limit condition:

Hope that I can hear your response for my question soon. Thanks in advance!

Your browser information:

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

Challenge: Exercise Tracker

Link to the challenge:

Hello,

when using res.json to send a response, try using original javascript object syntax.

For instance do not do:

res.json({
            '_id': user.id,
            'username': user.username,
            'count': log.length,
            'log': log
          });

instead do this:

res.json({
            _id: user.id,
            username: user.username,
            count: log.length,
            log: log
          });

The reason for doing it this way is that res.json method will automatically convert Javascript object format into JSON format.

I believe you are wrapping your object keys in unnecessary double quotes.

Let me know if this fixes your problem.

Thanks for your response.
But I try to omit the quotes in the res.json but it still fails at these two tests

Post the link you’re trying to submit, screenshots are not too helpful.

Repl.it - boilerplate-project-exercisetracker

This is my full project

My guess would be that the problem is in server.js on line 86:

  if (date === '') {

If date is not provided it’s not an empty string, it’s undefined.

1 Like

OMG thank you it solved my problem. Even though till now I can’t understand why the problem is in test 4 but then it failed at my test 5 and 6.
Anyway, thank you so much again :blush: