APIs and Microservices Projects - Exercise Tracker

Hello, I’ve got some troubles with this challenge.
Actually with one checking part of it,
‘I can add an exercise to any user by posting form data userId(_id), description, duration, and optionally date to /api/exercise/add. If no date supplied it will use current date. App will return the user object with the exercise fields added.’ if one of exercise’s submission conditions but it’s fails. However, on database i’ve got test records with all data added successfully.
Can anybody tell maybe this task condition has some other restrictions?

1 Like

Update: for me the mistake was that it expected the date to be with a zero in the front if it’s a single digit + it expected the duration to be a number and not a string.

Same, I get the same response as in the example exercise:

{"username":"dsdfsdfs","description":"000___000","duration":"12","_id":"5e7fd93413169a2c258aa95e","date":"Sun Mar 29 2020"}

Example:
{"username":"bbbbbbbbbbbbbbbbb","description":"bbbb","duration":1,"_id":"HJskEXTU8","date":"Sun Mar 29 2020"}

yet it doesn’t pass

I have the same problem. Could someone tell me what the response format is?

I tried:

{
  _id: 5e80c3496c9fd94383d75f75,
  username: 'fcc_test_15854969050',
  description: 'test',
  duration: '60',
  date: 'Sun Mar 29 2020'
}

Or:

{
  username: 'fcc_test_15854972958',
  description: 'test',
  duration: '60',
  date: 'Mon Jan 01 1990'
}

It does not pass the test.

I thought, it should be like in the example response. But, having identical one doesn’t help to fix it. Maybe the order of response’s object properties may role in testing… Like this one

{
username: "kavo",
description: "new one",
duration: 111,
_id: "SJi9pBoII",
date: "Sun Mar 29 2020"
}

doesn’t equal to this one in tests on apply.

{
_id: "SJi9pBoII",
username: "kavo",
description: "new one",
duration: 111,
date: "Sun Mar 29 2020"
}

My mistake was to use “duration” as “string” instead of “number”.

Thank you @s1oux.

2 Likes

I’ve also found solution for my trouble. That was incorrect date format.
Also thanks for paying attention.

I can’t pass this test either - the functionality is (I think) fine, but this one test will not pass. Please can one of you have a quick look and see what might be wrong with mine?

My code:

//// User Story 3
app.post("/api/exercise/add", (req, res) => {
  console.log(req.body);
  let input = req.body;
  // Check input contains required fields - send res message if not - set date to today/now if not given
  if (!input.userId || !input.description || !input.duration) {
    res.send(
      "User ID, Description and Duration are required fields - please enter values..."
    );
  } else if (!input.date) {
    input.date = new Date();
  }
  // Create object to push into exercise array in db
  let exerciseInstance = {
    activity: input.description,
    duration: input.duration,
    date: input.date
  };
  // Find user in db and update
  Data.findByIdAndUpdate(
    input.userId,
    { $push: { exercise: exerciseInstance } },
    (err, doc) => {
      if (err) return console.log("Error: ", err);
      res.json({
        username: doc.username,
        _id: doc._id,
        exercise: exerciseInstance
      });
    }
  );
});

In Mongo Atlas I can see entries where the fcc bot is testing and adding entries:

_id: "apUR9"
username: "fcc_test_15855768810"
exercise: [
0: {
_id: 5e81fbacadecd056dea2192e
activity: "test"
duration: 60
date: 1990-01-01T00:00:00.000+00:00
}
1: {
_id: 5e81fbacadecd056dea2192f
activity: "test"
duration: 60
date: 1990-01-02T00:00:00.000+00:00
}
]
__v: 0

1 Like

I think you should try to have strict order of properties in response payload, like in the example’s response object, and maybe you date format as in the example (by using toDateString() func on the date property) to pass this test:

{
username: "kavo",
description: "new one",
duration: 111,
_id: "SJi9pBoII",
date: "Sun Mar 29 2020"
}

Actually don’t know if it helps you. But I solved it in such way.

2 Likes

Thank you! This was the issue… I had my response object laid out differently so I changed it to this:

{
"username": "zedtest",
"description": "Squash",
"duration": "200",
"_id": "QR0qg",
"date": "Wed Feb 12 2020"
}

And then it still wouldn’t work, I needed to actually return an int for the duration instead of a string… so actual response - as you’ve already said above needs to be in this format:

{
"username": "zedtest",
"description": "Squash",
"duration": 200,
"_id": "QR0qg",
"date": "Wed Feb 12 2020"
}

Thanks again and happy coding :man_technologist::woman_technologist:

1 Like

Here is my Output

{
	"username": "uday",
	"description": "running",
	"duration": 10,
	"_id": "mu5ZjpvXy",
	"date": "Tue 28 Apr 2020"
}

yet cannot pass the test :face_with_raised_eyebrow:
duration is set to Number in db model

I have the same error, but I figured out that freecodecamp uses a deepEqual (just like ===) to compare the result, so if there is a slight difference in the return result the test will fail.
Anyway here is how I solved it, in the response json to be returned I wrote this

{username: user.username, _id: user._id, description: result.description , duration: result.duration ,  date: new Date(result.date).toDateString()}

Points:

  1. Return user id not exercise id
  2. Parse return date with .toDateString() to make it look in “Wed May 13 2020” format.
  3. Make sure you are passing duration from the one after saving to db not req.body, else sent response will be String not Number
4 Likes

Thanks a lot. this helped me.
Type of duration should be Number and Date should be converted using .toDateString() .
Also make sure you pass Date.now() to handle when date is not given by user, it should be current date.

1 Like

I second that thank you. :slight_smile:

" ```
{username: user.username, _id: user._id, description: result.description , duration: result.duration , date: new Date(result.date).toDateString()}