Back End Development and APIs Projects - Exercise Tracker

Tell us what’s happening:

Im trying to finish the challenge but unable to pass two test:
Test 8: Failed:The response returned from POST /api/users/:_id/exercises will be the user object with the exercise fields added.

Test 15: Failed: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.

Your code so far

 const response = {
      _id: user._id,
      username: user.username,
      description: newExercise.description,
      duration: newExercise.duration,
      date: new Date(newExercise.date).toDateString(),
    };
    console.log(response);
    res.json(response);
// Extract logs from the user and format the date
    logs = logs.map((log) => ({
      description: log.description,
      duration: log.duration,
      date: new Date(log.date).toDateString(), // Convert date to a readable format
    }));

    // Construct the response object
    const userLog = { _id: userId, username: user.username, count, log: logs };

    // Return the user object with the log array and a count of the total exercises.
    return res.json(userLog);

As shown in the image uploaded, all other tests are passing successfully. Have tried many ways and data output is fine; don’t know which solution the test is asking.

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36

Challenge Information:

Back End Development and APIs Projects - Exercise Tracker

1 Like

I have the exact same error. I hope someone could help us

1 Like

Ernesto its the UTC offset!!!

Probably caused by running the projects locally.

Try this

const checkDate = (date) => {
    if (!date) {
        return (new Date(Date.now())).toDateString();
    } else {
        const parts = date.split('-');
        const year = parseInt(parts[0]);
        const month = parseInt(parts[1]) - 1;
        const day = parseInt(parts[2]);

        const utcDate = new Date(Date.UTC(year, month, day));
        return new Date(utcDate.getTime() + utcDate.getTimezoneOffset() * 60000).toDateString();
    }
}
3 Likes
Awsome, that made it work.
So far I need to pass the following test:
Test 15: Failed: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.

Thanks so much :pray:t3:

1 Like

Guys after all I just confirm I passed the challenge!!!
Thanks to all of you who help either directly or with their own question and special to Ardilla.

1 Like

Getting my certificate soon…

1 Like

Hope can be doing good kind of you Ardila for putting this information it is strange that this can make it work for #8 when asking for user object and adding exercises.

Just wanted to add some information also if someone else can have a problem with this, me have tried for 4 days with (8 & 15) now and am finally close to finishing it because of the error.

Used the checkDate function made but it did not work for me unless removing the arrow function, using just the if else statement, and changing the “return” to assigning it to a “date” variable but this made it work for #8.

app.post('/api/users/:_id/exercises', async (req, res) => {
  console.log('post exercises can be working')
  const userId = req.params._id; 
  console.log('userparam id working')

  let { description, duration, date } = req.body; 

  // if(!date){
  //   date = new Date()
  // } else {
  //   date = new Date(date)
  // }

  //changing returns to date =  and removing const dateChecker fixed: 
  // The response returned from POST /api/users/:_id/exercises will be the user object with the exercise fields added.
    if (!date) {
        date = (new Date(Date.now())).toDateString();
    } else {
        const parts = date.split('-');
        const year = parseInt(parts[0]);
        const month = parseInt(parts[1]) - 1;
        const day = parseInt(parts[2]);

        const utcDate = new Date(Date.UTC(year, month, day));
        date =  new Date(utcDate.getTime() + utcDate.getTimezoneOffset() * 60000).toDateString();
    }

  let foundUser = await User.findById(userId); 
 
  const newExercise = new Exercise({
    username: foundUser.username, 
    description, 
    duration: Number(duration),
    date,
    userId: userId, 
  })

  await newExercise.save()

  res.json({
    _id: foundUser._id,
    username: foundUser.username,
    description: newExercise.description,
    duration: newExercise.duration,
    date: newExercise.date.toDateString(),
  })
})

Now need to fix #15:
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.

Also me fixed 15 after a few posts about changing time settings and checking your chrome inspector/network/logs/headers to find the date being used by freeCodeCamp to run test probably was a day ahead and then changed computer date and time to be close to it and it fixed problem, took 4 days to find this and learned many good things for Express, routes and coding while doing this kind of freecodecamp to offer this paper certificate.

Hopefully this can help someone else also.

Thank you take care