Unable to pass "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 "

Hi,

I’m unable to pass the above test in the exercise tracker project for 'back-end-development-and-APIs ’ certification although my code is working fine. Here’s the code I’m using:

const exerciseSchema = new mongoose.Schema({
  userId: String, description: String, duration: Number, date: String 
})

const Exercise = mongoose.model('Exercise', exerciseSchema)

app.post('/api/users/:_id/exercises', async (req, res) => {
    const id = req.params._id
    User.findById(id, (err, user) => {
      if (err || !user) {
        console.log(err)
      } else {
      const {description, duration, date } =           req.body
    const givenDate = new Date(date)
     Exercise.create({
      userId: id,
      description,
      duration,
      date: (givenDate == 'Invalid Date') ? new Date().toDateString() : givenDate.toDateString()
  }, (err, exercise) => {
      if (err || !exercise) {
        console.log(err)
      } else {
        const { date } = exercise
        res.send({
          username: user.username,
          description,
          duration: parseInt(duration),
          date,
          _id: user.id
        })
      }
  })
      }  
    }) })

app.get('/api/users/:_id/logs', (req, res) => {
  const id = req.params._id
  Exercise.find({userId: id }, (err, log) => {
    if (err) {
      console.log(err)
    } else {
      User.findById(id, (err, user) => {
        if (err) {
          console.log(err)
        } else {
          console.log(log)
          res.send({
            username: user.username,
            count: log.length,
            _id: id,
            log
          })
        }
      })
    }
  })
})

Can anyone please tell me what I’m doing wrong? Also please help me in locating the test cases file for this project so I can compare my code to the test I’m failing to pass.

Thanks

If this is the only failiing test, search for the forums for the time zone offset problem and try those solutions.

If that doesn’t work, log all your route inputs and responses to check that your code is doing what you think it’s doing. Logging the inputs will give you the test information you want.

Finally, if that doesn’t work, post a link to a repl (repl.it preferably) so we can see and run your code ourselves.

Thanks for the suggestion. I’ve just cleared all the tests.

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