Back End Development and APIs Projects - Exercise Tracker

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