Exercise Tracker - task#4

All my tests are OK except #4:
You can POST to /api/users/:_id/exercises with form data ‘description’, ‘duration’, and optionally ‘date’. If no date is supplied, the current date will be used. The response returned will be the user object with the exercise fields added.
(URL in index.html and tests are different)

I have following code (truncated):

const userSchema = mongoose.Schema(
  {
    username: { type: String, required: true, unique: false },
    exercices: [
      {
        description: { type: String },
        duration: { type: Number },
        date: { type: String, required: false }
      }
    ]
  }
);
const User = mongoose.model('Users', userSchema);

const defaultDate = () => new Date().toISOString().slice(0, 10);

function addExercise(req, res) {
  const userId = req.params.userId || req.body.userId; // userId come from URL or  body
  const exObj = { 
    description: req.body.description,
    duration: +req.body.duration,
    date: req.body.date || defaultDate()
  }; 
  User.findByIdAndUpdate(
    userId,
    {$push: { exercices: exObj } }, 
    {new: true},
    function (err, updatedUser) {
      if(err) {
        return console.log('update error:',err);
      }
      let returnObj = {
        _id: updatedUser.id,
        username: updatedUser.username,
        description: exObj.description,
        duration: exObj.duration,
        date: exObj.date
      };
      res.json(returnObj);
    }
  );
}

app.all("/api/users/:userId/exercises", addExercise);

debug show this:

  • POST to URL /api/users/607d75bee17ce700fff750fe/exercises
  • BODY: { description: ‘test’, duration: ‘60’, date: ‘1990-01-01’}

Object to return:

{
  _id: '607d75bee17ce700fff750fe',
  username: 'fcc_test_16188348755',
  description: 'test',
  duration: 60,
  date: '1990-01-01'
}

Test fails.
I can’t figure what is wrong.

github: https://github.com/LLPeterX/exercisetracker.git

Welcome there,

This is not the expected return of the date object. The best way to achieve the expected return type is with .toDateString() on the date object.

The endpoints were recently changed. Provided your server uses the ones given on the user stories/tests you will be fine.

Hope this helps

1 Like

Thanks it helped
In the following tasks (/api/users/:_id/logs) should the dates be in toDateString() format, or ‘YYYY-MM-DD’?

I do not think, in that test, it looks at the format. But, why not just keep them all the same?

Also, to point out, this is misspelt:

Thank you.
All solved.

1 Like

@aswonder please share your code i am unable to pass the tests from test#3 for the last 2 days

2 Likes

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