FCC Exercise tracker help

I am written and code for the project and in my testing everything is working prefectly and matches the output freecodecamp smaple project
Here is the glitch link

https://beneficial-eggplant-spice.glitch.me

and code file

https://glitch.com/edit/#!/beneficial-eggplant-spice

i cannot pass these tests : [test 3 and 4]

  1. I can get an array of all users by getting api/exercise/users with the same info as when creating a user.
    
  2. 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.

Hey @dev117uday, I had a look at your code and ran some tests. Apparently it’s all working indeed. Have you tried to post your code to FCC, and open the console on your Glitch project to see what errors or other things that might come up when FCC runs its tests against your code?

I was able to correct one of them :
but the other

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.

still having problem with

thanks for the advice , i look into glitch’s console.log to find out the last one.
#notgoingtosleeptoday

could this is problem of async and await ?

It could be. Looking at your code again, you aren’t using async/await appropriately. Try to remove them because the way your code is written, those statements are useless anyway.

async/await is not the problem. i tried console.log every step in route

/api/exercise/add

and it’s executing the order intented.

and sorry, i did put async/await in complete hurry just to solve it.

1 Like

Alright I think I found something.

else if (isNaN(dat)) {
    console.log("date isNan");
    res
      .type("txt")
      .send(`Cast to Date failed for value "${req.body.date}" at path "date"`);
  }

This part of your code is making the field date be required, when it should NOT. The exercise says:

  1. 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. Returned will be the user object with also with the exercise fields added.

In your mongoose schema, the date field type is Number. Change that field to type Date and you’ll be able to get rid of lots of ‘Number to String to Date’ confusing validations. You have to deal with the type Date for that field

1 Like

Realistically, in a pseudo-code, what that requirements is asking you to do is pretty much the following:

find the user by its id //User.findOneById({ _id: req.body.id });
found something? Push a new object Exercise to the User exercise log array 
Your User schema should have a field log of type array. Something like this:
/* log: [ {
    description: { type: String, required: true }, 
    duration: { type: Number, required: true }, 
    date: Date
  } ]
*/
2 Likes

Okay, thank you so much for helping.

I look into this.

thankx again

1 Like

well i correct my mistake and used number instead and it worked.
thank you