CastError: Cast to embedded failed for value "{ description: 'test', duration: 60, date: Invalid Date }"
...
at /home/runner/xciseTracker/index.js:65:32
because you’re not checking for missing dates properly. Log the route inputs and your date to see the problem. Since this is the POST exercise route which is used by some of the failing tests, this is likely a factor in their failure, despite their format.
looks like you have duplicate entries in your log as there should be only one for each day. You should also log the route inputs and your processed values of them here as well to make sure you are handling the optional parameters correctly.
Thank you so much for your advise. It finally passed all tests.
But I still have one concern. I changed to use the “save” approach to update the log, and it stops producing duplicate log. But I still don’t know why “findOneAndUpdate” function in Mongoose will produce duplicate objects. Do you have any idea?
findOneAndUpdate() won’t create duplicates by itself but it will if called twice if you are storing the exercise records as an array on the user object. It will find one user and then add the exercise record as many times as you call it. There was probably an error in a conditional that allowed findOneAndUpdate() to be called twice in certain code paths.