Problem with the add-exercise part of Exercise Tracker

I’m having a problem with the add-exercise part of the challenge. The code passes all the other tests except the 4th test:

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

This is my code for the add-exercise method:

app.post("/api/exercise/add", function(req, res) {
  const { userId, description, duration, date } = req.body;
  User.findOne({ _id: userId }, function(err, data) {
    const username = data.username;
    if (err) return console.error(err);
    var inputDate;
    if (!date || date == "") {
      inputDate = new Date()
        .toUTCString()
        .split(" ")
        .slice(0, 4)
        .join(" ");
    } else {
      inputDate = new Date(date)
        .toUTCString()
        .split(" ")
        .slice(0, 4)
        .join(" ");
    }
    if (inputDate == "Invalid Date") {
      return res.send("Invalid Date");
    }
    var exercise = {
      description: description,
      duration: parseInt(duration),
      date: inputDate
    };
    data.exercises.push(exercise);
    data.save(function(err, data) {
      if (err) return console.error(err);
      var output = {
        username: username,
        description: description,
        duration: parseInt(duration),
        _id: userId,
        date: inputDate.split(",").join("")
      };
      return res.json(output);
    });
  });
});

This is an example of the output that I log:

{ username: 'fcc_test_15888465158',
  description: 'test',
  duration: 60,
  _id: '6AfrD7Ybm',
  date: 'Mon 01 Jan 1990' }

I’ve seen other posts on this issue, and have changed the date to the string format and changed duration to an integer. I’ve been at this for days and I feel like I’m overlooking something obvious but I can’t figure out what.

Link to challenge: https://www.freecodecamp.org/learn/apis-and-microservices/apis-and-microservices-projects/exercise-tracker
Link to app: https://pebble-spiral-layer.glitch.me

Hello there.

This is what the tests are expecting as an output:

 const expected = {
          username,
          description: 'test',
          duration: 60,
          _id,
          date: 'Mon Jan 01 1990'
        };

Your date is the problem. It is not a major issue, but, unfortunately, the tests are not robust enough for any deviation.

Hope this helps

2 Likes

That works. Thank you :smiley: