Back End Development and APIs Projects - Exercise Tracker

Can’t Pass The 8th test :

the Problem:
although i see the response is identical to what freecodecamp implementation provide, i can’t pass the test !!
which is overwhelming :pensive:
thank you for your time in advance :blue_heart:

the test is :
Failed: The response returned from POST /api/users/:_id/exercises will be the user object with the exercise fields added.

the responsable code:

app.post('/api/users/:_id/exercises', (req, res) => {
  var id = req.params._id;
  var desc = req.body.description;
  var dur = Number(req.body.duration);
  var date = req.body.date == '' ? new Date(Date.now()) : new Date(req.body.date)

  Exercise.create({
    'date': date.toDateString(),
    'duration': dur,
    'description': desc
  })
  .then( async (exer) => {
    var ExId = exer._id;
    usr = await User.findById(id).exec();
    usr.logs.push(exer._id);
    usr.save()
    .then(async (usr) => {
      usr = await usr.populate({
        path: 'logs',
        match: {_id: ExId}
      })
      usr = usr.toObject({versionKey: false});
      result = {...usr, ...usr.logs[0]}
      delete result.logs
      res.json(result);
    });
  })
  .catch((err) => {
    if (err) throw err
  });
});

response Screenshots:

FreeCodeCamp solution:
you can find it in the challenge page down

Your project link(s)

solution: boilerplate-project-exercisetracker - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/110.0

Challenge: Back End Development and APIs Projects - Exercise Tracker

Link to the challenge:

Your current log output is telling you the problem where it has Invalid Date. Part of the offending code is

because an undefined date is not the same as an empty string. Many of the tests use an undefined date and expect the specified behavior. If you continue to have problems, log the route name, route input, and all route responses from every route and run the fCC tests.

You also need to change your mongoDB atlas credentials since they were in the repl you posted.

1 Like

Thank you so much. :smiley:
Actually you were right when you say the problem may be in the date,
but I couldn’t solve the problem without this advice:

If you continue to have problems, log the route name, route input, and all route responses from every route and run the fCC tests

this advice worth money :heart_eyes:
i believe every bug can be solved using it
Thank you also for the security tip
I really appreciete your efforts :blue_heart: