My Exercise Tracker API still not passing POST exercises test?


Referring to this closed thread:

I’m also struggling with this final test case:

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.

But I checked the test case source and still see nothing wrong with my solution.
Here are latest logged requests/responses:

Inserted user#4
Inserted exercise_log#3
reqBody: { description: 'test', duration: '60' }  => respBody: {
  username: 'fcc_test_16257977835',
  description: 'test',
  duration: 60,
  _id: '4',
  date: 'Fri Jul 09 2021'
at=info method=POST path="/api/users/4/exercises"
Inserted user#5
Inserted exercise_log#4
reqBody: { description: 'test', duration: '60', date: '1990-01-01' }  => respBody: {
  username: 'fcc_test_16257977844',
  description: 'test',
  duration: 60,
  _id: '5',
  date: 'Mon Jan 01 1990'
at=info method=POST path="/api/users/5/exercises"

My solution is at:

Please help. Thanks!

P.S. I use SQlite for database.

I had encountered a similar issue like yours, where I struggled with the same test case just now.
I think the problem lies in the _id field.

After trial and error for a while, I finally managed to pass the test cases. Here’s what I attempted:

  • The date is in the format similar to "Mon Jan 01 1990". I used toDateString() on Date object to achieve that
  • The duration must be of type Number and not in String

I can see that your response does meet the criteria above, but I did pass the test cases, and you didn’t. Therefore, the difference could be in the database used (I use MongoDB, and you mentioned that you use SQlite).

Here’s an example JSON returned from my server:

    "_id": "60e706ccfdb70939d0cd96f9",
    "username": "AdmiJW",
    "date": "Mon Jan 01 1990",
    "duration": 60,
    "description": "test"

Therefore, to pass the test case, I think the _id needs to be in MongoDB’s ObjectId format, issued by the database itself. You are using SQlite as your database, which issues id in incrementing numbers (1,2,3…).

Once I changed my code to return _id: 1 or _id: "1" , the test case failed again.

Hope this helps!

Thanks very much!

Now I passed the challenge, after utilizing MongoDB’s ObjectId function to generate user._id instead of auto-increment number.

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