Exercise Tracker test #4

Hey everyone, I’m almost finished with the exercise tracker! :smile: 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.

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

Here is my API for the add exercise:

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. I appreciate any suggestions you may have! Thanks a lot for all your help over the years! :smiley:

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.

Hope that helps

1 Like

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, but wasn’t able to find much promising information to advance any further on this project.

:thinking: :pensive:

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

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

1 Like