Cannot pass test 5 and 6 of APIS and microservices projects - Exercise Tracker

Tell us what’s happening:
Hello All,

The following tests failed:

  • test 5: You can make a GET request to /api/exercise/log with a parameter of userId=_id to retrieve a full exercise log of any user. The returned response will be the user object with a log array of all the exercises added. Each log item has the description , duration , and date properties.

  • test 6: A request to a user’s log ( /api/exercise/log ) returns an object with a count property representing the number of exercises returned.

Here is my test output for these tests:

// GET /api/exercise/log?userId=604c70ee41091601ca5a72bb
{"_id":"604c70ee41091601ca5a72bb","username":"nesta1","count":2,"log":[{"description":"exercise 1","duration":10,"date":"Sat Mar 13 2021"},{"description":"exercise 1","duration":20,"date":"Sat Mar 13 2021"}]}

Here is the test output from the FreeCodeCamp example (https://exercise-tracker.freecodecamp.rocks/):

// GET /api/exercise/log?userId=6045e4e56c1ac605e6029037
{"_id":"6045e4e56c1ac605e6029037","username":"nesta8","count":5,"log":[{"description":"exercise 1","duration":1,"date":"Fri Jan 01 2021"},{"description":"exercise 1","duration":1,"date":"Fri Jan 01 2021"},{"description":"exercise 2","duration":1,"date":"Fri Jan 01 2021"},{"description":"exercise 2","duration":2,"date":"Fri Jan 01 2021"},{"description":"test","duration":10,"date":"Tue Oct 10 2000"}]}

Both outputs are similar.

Test 7 is a pass. How can it pass and test 5 and 6 fail?
You can add from , to and limit parameters to a /api/exercise/log request to retrieve part of the log of any user. from and to are dates in yyyy-mm-dd format. limit is an integer of how many logs to send back.

Your project link(s)

solution: https://boilerplate-project-exercisetracker.stephaneramael.repl.co

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36.

Challenge: Exercise Tracker

Link to the challenge:

Many thanks in advance,

Line 53 - what happens when date is not provided?

Hi Jenovs,

When date is not provided in the form, the current date is used.

//POST /api/exercise/add
{"_id":"604c70ee41091601ca5a72bb","username":"nesta1","date":"Sun Mar 14 2021","duration":20,"description":"exercise 1"}

The code taking care of it is:

  if (req.body.date === '') req.body.date = new Date();
  else req.body.date = new Date(req.body.date);

I use the following Schemas.

const logSchema = new Schema({
    description: { type: String, required: true },
    duration: { type: Number, required: true },
    date: Date
  });
const userSchema = new Schema({
  username: { type: String, required: true },
  log: [{type: logSchema, required: true}]

Are you sure that if the date is not provided this condition will be true?
Add console.log and check what is the value of data.

I am not sure if I follow you about Add console.log and check what is the value ofdata ..

I added the console.log.

  if (req.body.date === '') req.body.date = new Date();
  else req.body.date = new Date(req.body.date);
  console.log(req.body.date);

It shows:
2021-03-14T11:33:06.027Z

That’s because you’re submitting via form which handles empty input as an empty string, but if you call api directly (what FCC tests do), date will be undefined.

1 Like

I was not aware or understood the way that FCC run the tests.

I will add the condition . It should be something like:

  if (req.body.date === '' || typeof req.body.date === 'undefined') req.body.date = new Date();
  else req.body.date = new Date(req.body.date);

Hi jenovs,

It works! Thanks again for this insightful information. I will never stop learning.

That’s the most common way you use an API. Learn about tool like Postman to test your APIs (https://www.postman.com/)

Also this:

  if (req.body.date === '' || typeof req.body.date === 'undefined') req.body.date = new Date();

could be reduced to:

if (!req.body.date) req.body.date = new Date();
1 Like

I will definitely read and learn more on the subject. Thanks for pointing this out to me.
I greatly appreciate it!

Thanks for the tip about reducing the code.

hi, it seems like u passed test case #4.

can u help me with the output type and all that’s expected from test case #4?

any kind of help would mean a lot!

thank you! :slight_smile:

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