Exercise Tracker not passing, help please


I’m having issues with the Exercise Tracker, specifically the

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

And here is what my res.json looks like on the POST:

{"username":"Joachim","description":"running","duration":20,"date":"Fri Sep 10 2021","_id":"0"}

Obvisouly, I’ve missed something but I truly can’t find what. I’ve checked other posts on here and have checked that my date is correct,

  • I’ve checked its DD for the date of the day. I’ve checked its 1990-01-01 when its supposed to, although the last POST by FCC is 1990-01-02 (thats what it prints when i log what i get in the post for the last exercise created by FCC).
  • I’ve checked that the duration is an integer, and the rest are strings.
// Adding an "Exercise" to a user
app.post("/api/users/:_id/exercises", (req,res)=>{
  // Get index of user
  const indexOfUser = usersLibrary.findIndex(i => {
    if(parseInt(i._id) == parseInt(req.params._id)) return true;
    else return false;
  console.log("Date logged by the POST:");
  console.log(req.body); // Logging DATE received by POST

  // Check if valid DATE
  let dateEntry;
  if(!isValidDate(new Date(req.body.date))) dateEntry = new Date();
  else  dateEntry = new Date(req.body.date);
  dateEntry = dateFns.format(dateEntry, "EEE MMM dd yyyy"); // Correct format
  console.log("Date after formatting: " + dateEntry); // Logging what I "save"
  // Adding to the count of logs
  // Adding the exercise to the LOG array of that user
  usersLibrary[indexOfUser].log.push({description: req.body.description.toString(), duration: parseInt(req.body.duration), date: dateEntry});
  // Sending answer
            username: usersLibrary[indexOfUser].username,
            description: req.body.description,
            duration: parseInt(req.body.duration),
            date: dateEntry.toString(), // Tried forcing all dates to "Mon Jan 01 1990"
            _id: usersLibrary[indexOfUser]._id.toString()

I’m at a loss >.<

p.s I haven’t used a database but just go from an array in memory to ensure no mistakes where done with the database.

If you can find a typo i’ve missed or something that’s off i’d be super grateful. Stuck 3 days now and everything else passes the test. I’m stumped X(

My Exercise Replit

Replying to myself because I finally found a solution, and hopefully this may help someone else!

The exercise nowhere states it requires the use of MongoDB, especially the use of ObjectId as the “_id” for the user objects, BUT, it IS required! I did see someone else mentioning that in a different thread and therefore decided to try it too.

That FIXED it and it finally passed the test. So if you’ve tried everything else but haven’t used MongoDB then try using the “mongodb” ObjectId for your “_id”.

** Pay attention that in the latest version of Mongo DB the last “d” in the class constructor name is a small “d” and not a capital “D”, so when trying to add it to your project use:

const ObjectId = require(‘mongodb’).ObjectId;

that took me a while!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.