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 https://glitch.com/edit/#!/seed-jet-dugong
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.
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.