Exercise Tracker App (Not passing tests)

Tell us what’s happening: I can’t pass FreeCodeCamp’s tests and I don’t know why. Can somebody please check lines 73-175 in server.js file?

Your code so far

Link to the app

Tests not passing

  • 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.
  • I can retrieve a full exercise log of any user by getting /api/exercise/log with a parameter of userId(_id). App will return the user object with added array log and count (total exercise count).

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.97 Safari/537.36.

Challenge: Exercise Tracker

Link to the challenge:

1 Like

Can you make your logic more simple for posting exercise info like this

  • As you have previous registered users
    at first you have to find that user from database

  • Then you will add $push exercise information to the exercise array under that username

  • if such user is not found, you will return error

  • if user found, it will add necessary data and
    then you will return expected json data

For the purpose you can use something like this:

yourDatabase.findOneAndUpdate({_id: userIdHere}, {
    $push: {
        yourExerciseFieldName: {
          //  put all your exercise info here as per your Schema 
}, (err, data) => {
    if (data == null) {
      //  send your custom error message here
    } else {
     //   send json data with required fields here

Hope this will help you for your first issue.
If it works, let me know, I’ll help you for the second issue if necessary.

Thank you.

1 Like

Edit. This code is in fact working. Either Glitch or my network was not working properly Yesterday. Today it’s fine.

I also changed if(err) to if(data == null).

I am not using console.log(data), that was just to illustrate what was happening. :relieved:

Thank you so much for your reply.

I think in the past I tried your approach, but glitch got stuck"thinking", just like now.

In any case, this is what I got so far. It’s not working. Maybe the reason is very obvious, but I can’t see it at the momment. I need to rest my eyes.

More coming Tomorrow! :grin:

//Add Exercise v2.
app.post('/api/exercise/add', (req, res) => {
  let {userId, description, duration, date} = req.body;
  date === "" ? date = new Date() : new Date(date);
  ExerciseUsers.findOneAndUpdate({_id: userId}, {
    $push: {
      log: {
        description: description,
        duration: duration,
        date: date
  }, {
    returnNewDocument: true
  }, (err, data) => {
    if(done == null) {
      console.log("There was an error.")
    } else {
1 Like

You are almost there, try again with the example code given above, It will work :grinning:

1 Like

Finally! Tests passed! This is a link to my final code:

I have to say FreeCodeCamp is a little bit picky about what passes and what doesn’t. :stuck_out_tongue_winking_eye: I spent the last hour changing names. :rofl:

1 Like