Back End Development and APIs Projects - Exercise Tracker

Tell us what’s happening:
I cannot pass the 10-15 tests, but my console log and the browser show the correct result in the required format.

Anyone can help me to find out the problem? Many thanks.
Attached is my console log and browser returned data.
console log
browser returned data

Your project link(s)

solution: https://xcisetracker--liamouyang.repl.co

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0

Challenge: Back End Development and APIs Projects - Exercise Tracker

Link to the challenge:

Can you show the api request for that specific output?

Please see below for the request. Thanks,
https://xcisetracker--liamouyang.repl.co/api/users/635172d07c4a9000c01b4a09/logs

You’ll need to post the link to your repl with the code, not the live version. We’ll need to see code to debug the problem.

Sorry for my misunderstanding. Here it goes.
Thanks.
https://replit.com/@LiamOuyang/xciseTracker#index.js

First, you’re getting this error on the server

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.

Second, this output from your GET log route

{
  username: 'fcc_test_16663938574',
  count: 4,
  _id: 63532701f44e2e004c152f10,
  log: [
    { description: 'test', duration: 60, date: 'Mon Jan 01 1990' },
    { description: 'test', duration: 60, date: 'Mon Jan 01 1990' },
    { description: 'test', duration: 60, date: 'Wed Jan 03 1990' },
    { description: 'test', duration: 60, date: 'Wed Jan 03 1990' }
  ]
}

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.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.