# Exercise Tracker test #4

**URL:** <https://forum.freecodecamp.org/t/exercise-tracker-test-4/416814>\
**Category:** General\
**Created:** [August 23, 2020, 12:23pm UTC](https://forum.freecodecamp.org/t/exercise-tracker-test-4/416814 "2020-08-23T12:23:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![camchardukian](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camchardukian/32/56045_2.png) [@camchardukian](https://forum.freecodecamp.org/u/camchardukian)\
**Post date:** [August 23, 2020, 12:23pm UTC](https://forum.freecodecamp.org/t/exercise-tracker-test-4/416814/1 "2020-08-23T12:23:20Z")

</div>

Hey everyone, I’m almost finished with the exercise tracker! 😄 I am able to pass all of the tests for this project except for the 4th one.

> 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.

While I have seen other users have failed this test due to using _userId_ instead of _\_id_ or forgetting to format the date, I’m not sure what is causing me to fail this test. I have seen my app seems to return an object formatted in the exact same way as the demo app.

```auto
{"_id":"5f4255a283c84000a74c15ee",
"username":"hobo22",
"date":"Fri Aug 14 2020",
"duration":100,
"description":"hoooo heeyyy"
}

```

Here is my API for the add exercise:

```auto
app.post("/api/exercise/add", async function(req, res) {
  const { userId, description, duration, date } = req.body;
  let exerciseDate;
  if (date) {
    exerciseDate = moment(date).format("ddd MMM D YYYY");
  } else {
    exerciseDate = moment().format("ddd MMM D YYYY");
  }
  console.log("exerciseDate", exerciseDate, typeof exerciseDate);
  const userArray = await user.find({ _id: userId });
  const userObject = userArray[0];
  const exercise = {
    date: exerciseDate,
    description: description,
    duration: parseInt(duration)
  };
  const exerciseArray = userObject.exercise_logs;
  exerciseArray.push(exercise);
  user.findOneAndUpdate(
    { _id: userId },
    { exercise_logs: exerciseArray },
    {
      new: true
    },
    (err, data) => {
      if (err) {
        console.log(err);
      }
      res.json({
        _id: userId,
        username: userObject.username,
        date: exercise.date,
        duration: exercise.duration,
        description: exercise.description
      });
    }
  );
});

```

Here is a [link to my project](https://glitch.com/~fcc-exercise-tracker-cameron). I appreciate any suggestions you may have! Thanks a lot for all your help over the years! 😃

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [August 23, 2020, 5:44pm UTC](https://forum.freecodecamp.org/t/exercise-tracker-test-4/416814/2 "2020-08-23T17:44:11Z")

</div>

Hello there,

You have done very well, in this project. Unfortunately, I think the tests are looking for an _ever-so-slightly_ different format in the `_id`:

I believe it should be an `IdObject`.

> [@Exercise Tracker Project - Oddly Unfulfilled User Story](https://forum.freecodecamp.org/t/exercise-tracker-project-oddly-unfulfilled-user-story/413550/8):
>
> Something that might be worth a try: const {ObjectId} = require('mongodb'); I have no experience with const SchemaTypes = mongoose.Schema.Types;, but I know the above method to create an ObjectId works. Just to help you debug, this is the expected user object in full: const expected = { username: 'fcc\_test\_1596648410971', // Obviously the numbers change description: 'test', duration: 60, \_id: 5f29cd9e782d5f13d127b456, // Must be of type 'ObjectId' date: 'Mon Jan 01 1990' }

Hope that helps

---

<div class="post-metadata">

**Author:** ![camchardukian](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camchardukian/32/56045_2.png) [@camchardukian](https://forum.freecodecamp.org/u/camchardukian)\
**Post date:** [August 24, 2020, 1:31pm UTC](https://forum.freecodecamp.org/t/exercise-tracker-test-4/416814/3 "2020-08-24T13:31:25Z")

</div>

First of all, thank you very much for your reply Sky @Sky020 . Your post that mentioned that \_id must be an object and your explanations of the unit tests were very helpful.

With that being said, I’ve just spent about 45 minutes trying to pass objects instead of strings without any luck. Would it be possible for me to ask for another hint, or for you to point me to some relevant reference materials?

I tried reading the official documentation for [Mongodb ObjectId](https://docs.mongodb.com/manual/reference/method/ObjectId/), but wasn’t able to find much promising information to advance any further on this project.

🤔 😔

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [August 24, 2020, 3:40pm UTC](https://forum.freecodecamp.org/t/exercise-tracker-test-4/416814/4 "2020-08-24T15:40:39Z")

</div>

Hey,

I am sorry you have had trouble with this project. Hopefully, you have learnt along the way.

I forked your project, and changed the following:

1. `const _id = ObjectId(userId)` I removed the `toString()` method on this.
2. I copy pasted what I had in my project for the date:

```auto
let exerciseDate = date ? (new Date(date)).toDateString() : (new Date()).toDateString();

```

I take it the `moment` package returns the incorrect type of date object.

This allowed me to pass. Hope it helps you.

If you get stuck: [https://glitch.com/edit/#!/amplified-curse-becklespinax](https://glitch.com/edit/#!/amplified-curse-becklespinax)

---

<div class="post-metadata">

**Author:** ![camchardukian](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camchardukian/32/56045_2.png) [@camchardukian](https://forum.freecodecamp.org/u/camchardukian)\
**Post date:** [August 25, 2020, 1:20pm UTC](https://forum.freecodecamp.org/t/exercise-tracker-test-4/416814/5 "2020-08-25T13:20:15Z")

</div>

> [@Sky020](#):
>
> I take it the `moment` package returns the incorrect type of date object.
> 
> This allowed me to pass. Hope it helps you.

Your comments in this thread are so much appreciated. I learned a lot building this project, and will continue working hard to learn more! 😃 Thank you so much for helping me on my journey!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:42pm UTC](https://forum.freecodecamp.org/t/exercise-tracker-test-4/416814/6 "2021-06-01T18:42:54Z")

</div>

Closed on June 1, 2021, 6:42pm UTC


