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

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

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();
    }
}
1 Like
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:

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.

Getting my certificate soon…