Exercise Tracker Tests Failing

Hello everyone!
It is the last certification i need to get and my 4,5,6 tests keep failing.
I have already looked into similar cases in forums, i tried changing bits and pieces
in my code, i changed _id with userId and vica versa in all places.

At the moment I’m at 4 hours refactoring even though everything is working as intended (or at least that’s what i think :smiley: ).

I could really use some help! Thanks for your time in advance!

Link: https://glitch.com/~beros-exercise-tracker

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36.

Challenge: Exercise Tracker

Link to the challenge:

The route path for exercise logs which you’ve defined is /api/exercise/users/log is incorrect. It should be /api/exercise/log.

And for the fourth test, the response should contain _id property which should be equal to the user’s ID.
So you just need to edit line 135 from userId: user._id to this _id: user._id
Changing userId to _id is not intended and is a bug. You just need to do it as is for the sake of passing the test here.

Thank you for your help! How careless of me to define to wrong path, which was what was causing 5,6 tests to fail.

I edited line 135 though and I’m still getting a failed 4th test. I even tried using another method in case something was off:

app.post("/api/exercise/add", (req, res) => {
  let { userId, description, duration, date } = req.body;  
  date = date ? new Date(date) : new Date()

  //   ADD exercise
  let addExercise = {    
    description,
    duration,
    date: date.toDateString()
  };
  
  //   Search USER to ADD exercise
  User.findOne({ _id: userId }, (err, user) => {
    
    if (err) res.send("Database error")
    if (user === null) res.send(`UserId: ${userId} - not found`)    
        
    user.log.push(addExercise);
    user.save(err => {
      if (err) res.send("Error adding exercise "+err);
      res.json({
        _id: userId,
        description,
        duration,
        date,
        username: user.username
      });
    })    
  })
});

Try using date: date.toDateString() in res.json. I know it’s a silly thing, but as I said, the fourth test is kinda broken at the moment.

Thanks again for your time and effort !

The bug was at the json after adding the exercise, the description was returning String where it should be returning Number, and even though I had type set to Number at input field and model, it was still giving back a string. Forced it here to get the correct output:

app.post("/api/exercise/add", (req, res) => {
  let { userId, description, duration, date } = req.body;  
  date = date ? new Date(date) : new Date()
  duration = Number(duration)
  ...
  ..
  .
}

Or at least that is what worked for me, I hope they fix this in the future!

1 Like