Back End Development and APIs Projects - Exercise Tracker

Tell us what’s happening:

Describe your issue in detail here.
It’s been about 4 days I’ve been struggling with question number 8. As a matter of fact, I am pretty sure that I got it right as I was able to return an object exactly as it described in the exercise.Yet , I can’t still pass that question number 8.

Your code so far

//exercise schema
const exercises = new mongoose.Schema({
  user_id: { type: String, require: true },
  description: { type: String, require: true },
  duration: { type: Number, require: true },
  date: { type: Date, default: new Date() },
});

// exercise model
const exerciseModel = mongoose.model("exercises", exercises);

app.post("/api/users/:_id/exercises", async (req, res) => {
  const userId = req.params._id;
  const exerciseObj = {
    userId,
    description: req.body.description,
    duration: req.body.duration,
  };

  if (req.body.date != "") {
    exerciseObj.date = req.body.date;
  }

  //const exercise = new exerciseModel(exerciseObj);

  const doc = await userModel.findById(userId);
 
  try {
    if (!doc) {
      res.json("The user doesn't exist in our db");
    } else {
      const exercise = new exerciseModel(exerciseObj);
      const savedExercise = await exercise.save();
      
      res.json({
        username: doc.username,
        _id: doc._id,
        description: savedExercise.description,
        duration: savedExercise.duration,
        date: savedExercise.date
          ? exercise.date.toDateString()
          : new Date().toDateString(),
      });
    }
  } catch (e) {
    console.log("er", e);
  }
});

Your browser information:

Below is the return object from my code

{

"username": "john doe",
"_id": "657dbbf8bd28899334a8e667",
"description": "d",
"duration": 1,
"date": "Sat Dec 16 2023"

}

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

property representing the number of exercises that belong to that user.

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Challenge Information:

Back End Development and APIs Projects - Exercise Tracker

Please post a Replit or a GitHub repo with your code so we can test it.

Open the browser developer tools and go to the network tab. Now submit your code and find the POST, go to the response tab, and post the response object you are sending for that POST test.


  if (req.body.date != "") {
    exerciseObj.date = req.body.date;
  }

Log out req.body.date and submit (do not use the form, do the submit). Make sure it has the value you are expecting it to have.

What you are doing with the date for the response should be done for the DB save. You should be checking the date value for the DB save, not just the response. You should save it as the date that was given or the current date.

sorry for delay Lasjorg.
Here is my github repo:

Did you fix it?


For me at least it was an issue with having the Schema type for the date be of type Date but saving it using toDateString. It caused some timezone issues where 1990-01-01 became 1989-12-31T23:00:00.000+00:00.

Removing toDateString from the actual model/DB code and only using it for the response let me pass all the tests using your code.

hi lasjorg
I am still struggling with it.
I would like to know what type you pass to date when you were creating the schema.
I also tried as you explained but still not passing.

First I would suggest you delete your collections on MongoDB Atlas to have a clear slate.

When you create the model you shouldn’t use toDateString but you still need to create the date as a date.

const exerciseDoc = new exerciseModel({
  username: user.username,
  user_id: user._id,
  description: body.description,
  duration: body.duration,
  date: req.body.date ? new Date(req.body.date) : new Date(),
});

You are sending the response correctly by using toDateString on the saved date.


As an aside, I’m not sure how I feel about relying on Mongoose to do type conversion. If you need duration to be of type number I would suggest doing it explicitly. It isn’t about the running code, it is about reading it. If I see duration saved as a string but it is a number because of the schema, it creates a disconnect in the code. I would be explicit about your types when creating models.

Also, this has to be online. You can’t pass certificate projects running locally.

Thank for your insight and contribution, I noticed I am not getting the expected time. for instance , when i enter 2022-12-12 in the input, instead of getting … Dec 12 2023, this is what I am getting :Mon Dec 11 2023. It looks like toDateString() is not return right date. But I could not find a way to get around it or solve the issue. i tried all other methods yet didn’t get the right one to solve the issue.
Any idea that will help me solve this headache is welcomed.
thanks