Exercise Tracker - eighth test

HI.

I’m doing the Exercise Tracker challenge. All tests pass successfully, except the eighth, the one that says:

The response returned from POST /api/users/:_id/exercises will be the user object with the exercise fields added.

It seems to me that this is precisely the behavior of the application: the response of the example application is

{
  "_id": "61204ee9f5860e05a3652f11",
  "username": "fcc_test_16295073016",
  "date": "Wed Jul 03 2019",
  "duration": 42,
  "description": "test for post"
}

while my application’s response is

{
  "username": "myself",
  "description": "first test for posting data",
  "duration": 42,
  "date": "Wed Jul 03 2019",
  "_id": "66c2224d1a07019654f170e8"
}

Below is the query that is called by my application:

const createAndSaveExercise = (exercise, done) => {
  const id = exercise._id;
  // search for an existing id
  models.userModel
    .findById(id)
    .then((doc) => {
      if (doc == null) {
        // if it doesn't find it does nothing
        done(null, null);
      } else {
        // else create and save an exercise for that id
        let { _id, description, duration, date } = exercise;
        let username = doc.username;

        date = new Date(date).toDateString();
        if (String(date).toLowerCase() == "invalid date") {
          date = new Date(Date.now()).toDateString();
        }
        let newExercise = new models.exerciseModel();
        newExercise.userId = _id;
        newExercise.description = description;
        newExercise.duration = duration;
        newExercise.date = date;
        newExercise
          .save()
          .then((doc) => {
            doc.username = username;
            done(null, doc);
          })
          .catch((err) => {
            done(err);
          });
      }
    })
    .catch((err) => {
      done(err);
    });
};

and then the function that manages the routing:

app.post("/api/users/:_id/exercises", urlEncodedParser, (req, res) => {
  const exercise = {
    _id: req.params._id,
    description: req.body.description,
    duration: req.body.duration,
    date: req.body.date,
  };
  dbmanager.createAndSaveExercise(exercise, (err, data) => {
    if (err) {
      res.json({
        error: `error creating a new exercise: "${err}"`,
      });
    } else {
      if (data) {
        const { username, description, duration, date, _id } = data;
        res.json({
          username,
          description,
          duration,
          date: new Date(date).toDateString(),
          _id,
        });
      } else {
        res.json({
          error: "there isn't any user with the supplied id",
        });
      }
    }
  });
});

What could be the problem?

Please post a repo with your code.

Hi @lasjorg, here’s the repo:

Your date might be wrong as well. At least I get two different dates depending on what I use. The payload date is correct (the one constructed from req.body.date) but the date that comes back from the saved document is wrong.

date = new Date(date).toDateString();
console.log("payload date", date);
// payload date Mon Jan 01 1990

const { username, description, duration, date, _id } = data;
console.log("db date", date);
// db date 1989-12-31T23:00:00.000Z

Probably daylight saving

Hope can be good don’t know if me can be correct but from having fun and learning this for 4 days I think I remember somewhere when trying to find a fix for my code someone with this type of date problem had it fixed by changing the computer date or time settings dont know if this is what is needed but wanted to add thank you take care.

I’m having the exact same issue with this and test #15. I checked in the DB and my logs have the date property set to the toDateString() return value, and my responses are also returning dates in the same format. I’ve logged the typeof date, and it’s string, so I’m really unsure of what’s going on, and of course, you don’t know why your application fails to pass the tests.

@Johnsoct If you are running it locally, try on Gitpod, Glitch, or Replit. Some people have had better success with date requirements on these platforms.

The issue may not be with the format or type, but the actual date returned.

I would suggest you create your own thread and post a link to a repo.

Edit: I guess you already did and fixed it as far as I can tell.

1 Like

Yeah, it’s related to my local timezone vs where the tests are being run… Once I figured it out, I preferred to just figure out the localhost solution since I did a lot of timezone work at my last company.