Exercise tracker project - stuck on 5th point

Hi guys,

I’m currently working on the APIs and Microservices certification.
I’ve got stuck in the Exercise tracker challenge, at the 5th point:

I can retrieve a full exercise log of any user by getting /api/exercise/log with a parameter of userId(_id). App will return the user object with added array log and count (total exercise count).

My output is identical to the one present on the FCC project example.

My code:

app.get('/api/exercise/log', function(req, res){
  const id = req.query.userId;
  User.findById(id, function(err, record){
    if(err) return console.log(err);
    let fmtLog = record.log.toObject().map(function(element){
      element.date = element.date.toDateString();
      return element;
    });
    res.json({
      _id: record._id,
      username: record.username,
      count: record.log.length,
      log: fmtLog
    });
  });
});

Can you locate my mistake?

Many thanks,
Marco

Hello there,

Unfortunately the tests are quite specific, and yours fails on a minor detail:

const logRes = await fetch(url + `/api/exercise/log?userId=${_id}`);
          if (logRes.ok) {
            const { log } = await logRes.json();
            assert.isArray(log);
            assert.equal(1, log.length);

log.length for yours is 2. Are you adding more exercises than you should be?

Hi,

I understand.
No, I’m not adding more exercises myself.
The testing bot does that.


Also, if the value of log.length needs to be 1, then what is the point of the count property?

Well, you are setting the count property. So, theoretically, you could be setting it incorrectly. Therefore, it is better the tests make use of the actual expected value.

The “bot” is not doing this. Here is the page showing the tests: https://github.com/freeCodeCamp/freeCodeCamp/blob/master/curriculum/challenges/english/05-apis-and-microservices/apis-and-microservices-projects/exercise-tracker.english.md

For this test, a new user is added, and one exercise is added, before the check for the number of exercises is passed. So, you might need to clear your database, and try the test again.

I cleared the whole database and ran the tests again. Still couldn’t pass the 4th user-story. My output is identical to FCC’s so I don’t really know what would be the problem. Maybe the user GETS the log before the exercises are processed in MongoDB Atlas?

Marco

I’ve retried the test a couple of times and still doesn’t work.
The output is the same as the model provided by FCC.
The ‘duration’ property is a number, the date is converted to a string using toDateString(), the 'log' property is an array of objects…
I don’t know what would be the cause of this problem.
Anyone has any indication?

Marco

Could you share the link to your project?

Sure!
Have a look: https://boilerplate-project-exercisetracker-1.marcodefusto.repl.co/

Many thanks,
Marco

Hey there,

It has taken a lot to debug this, but your issue is here:

req.body.date == "" ? new Date() : new Date(req.body.date.toString())

This is not doing what it should be, when there is no Date given, and is returning an error.

PS: I was only able to debug this through Postman. If you want to check it out.

Hope this helps

Thank you so much!
I was stuck in this project for several weeks.

The problem was, as you’ve pointed out, when the user didn’t specify a date.
The mistake was at my condition:

req.body.date == "" ?

I swapped the ternary operators with an if-else statement (which I am more confident with) and modified the condition:

if(req.body.date) 

I think the problem was that my original condition was checking whether or not req.body.date was an empty string, when it should’ve been checking whether or not that value was a falsy in order for it to successfully generate a Data if the user doesn’t specify one.

I will check Postman out after sending this reply.

Best regards,
Marco