Exercise Tracker 4th test

can’t pass 4th test even though the response seems okay but I don’t get why it doesn’t pass
here’s my code for the 4th test:

  let userId = req.params._id
   let newExerciseItem = new Exercise({
    description: req.body.description,
    duration: parseInt(req.body.duration),
    date: req.body.date
  })
  
  if(newExerciseItem.date === ''){
    newExerciseItem.date = new Date()
  }
  
  User.findByIdAndUpdate(
    userId,
    {$push: {log: newExerciseItem}},
    {new: true},
    (error, data) => {
    if(!error){
      let resObj = {}
      resObj.username = data.username
      resObj._id = data.id
      resObj.description = req.body.description
      resObj.duration = parseInt(req.body.duration)
      resObj.date = newExerciseItem.date
      res.json(resObj)
    }
  })
})

and here are my Schemas

let exerciseSchema = Schema({
  description:{type:String,required:true},
  duration:{type:Number,required:true},
  date:{type: Date}
})
let userSchema = Schema({
  username:String,
  count:Number,
  log:[exerciseSchema]
})

let Exercise = mongoose.model('exercise',exerciseSchema)
let User = mongoose.model('user',userSchema);
let resObj = {};

Please help, at this point, I have no idea of what’s wrong

Welcome there,

The expected date format is a Date string - hint: look at the Date methods available.

Otherwise, would you mind sharing a link to:

  1. An online, public, live version of your app
  2. All your code

It is difficult to help without that.

Hope this helps

sure! here’s my code Glitch :・゚✧
I changed exercise.date format but it still doesn’t pass the test

okay, I’ve figured it out! date type for Schema should be Date, so you don’t have to format it to String while creating an exercise item, but you have to convert it to String while sending it as json object in response

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.