Exercise Tracker last test

I cannot pass the last test. I see the correct things according to the spec:
image
but am not passing the last test:

// running tests
You can add from, to and limit parameters to a GET /api/users/:_id/logs 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.
// tests completed

Here is my link: https://boilerplate-project-exercisetracker.alanbra.repl.co

This is the project: https://www.freecodecamp.org/learn/back-end-development-and-apis/back-end-development-and-apis-projects/exercise-tracker

I have tried all sorts of things and have looked at lots of other threads on the same subject and tried various suggestions but cannot find anything that works. I am using Cluster0 from Mongo Atlas with a Ireland (eu-west-1) region.

A search of the forum yields this thread that I replied to:

Thank you for replying.

I changed my code to:

exercise.find({userid: userid})
  .select({ userid: 0, _id: 0, __v: 0 })
  .then(function(recs) {
  if (recs.length === 0) {
    res.json({ error: 'no exercises for this user in database!',
               username: username,
               _id: userid
             });
  }
  else {
    const arr = [];
    const fDate = new Date(from);
    const tDate = new Date(to);
    const fromDate = new Date(fDate.getTime() +
                              fDate.getTimezoneOffset() *
                              60000
                             );
    const toDate = new Date(tDate.getTime() +
                            tDate.getTimezoneOffset() *
                            60000
                           );
    for(let i in recs) {
      const d = new Date(recs[i].date);
      const dDate = new Date(d.getTime() +
                             d.getTimezoneOffset() *
                             60000
                            );
      if ((dDate >= fromDate) &&
          (dDate <= toDate) &&
          (arr.length < limit)) {
        const o = { description: recs[i].description,
                    duration:    recs[i].duration,
                    date:        d.toDateString(),
                  };
        arr.push(o);
      }
    }
    res.json({ username: username,
               count: arr.length,
               _id: userid,
               log: arr
             });
  }
})

but it still doesn’t pass the test.

My timezone is
image

I’ve reworked my project - I’ve changed it a lot but I still cannot pass the last test - please someone help

My code: boilerplate-project-exercisetracker - Node.js Repl - Replit

My replit: boilerplate-project-exercisetracker - Node.js Repl - Replit

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

Challenge: Back End Development and APIs Projects - Exercise Tracker

Link to the challenge:

I cannot pass the last test:

You can add from, to and limit parameters to a
GET /api/users/:_id/logs 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.

I have tried all sorts of things, such as:
having the date in the exercise schema being a date type,
using getTime(),
using toISOString() and toUTCString()
changing my computers timezone to every timezone and running the test in each timezone,
trying different ways of getting rid of any time element to avoid time differences between my location
image
and wherever the tests are run from

I have tried using Chrome, Edge and am currently using Firefox
I have tried writing my program in Visual Studio Code

all to no avail :frowning:

My current code has the date in the exercise schema as a Number type and storing the data (when posting) as yyyymmdd (eg 20221021) and then turning the from and to parameters into the same format for comparison.

I have been trying to pass this for the last 2 weeks :frowning:

I would really appreciate any help in progressing

Thanks
Alan

(I asked this under Backend Development but the only help suggested timezone issues which I have tried as mentioned above - I am asking this in this subforum in case someone here has any ideas)

You’re not logging the route inputs and outputs. You need to do that from your POST exercise and GET logs routes. And you need to log the output before every single res.json(...) in the GET logs route because there are a bunch of them. I partially did this and got the following output

POST exercise
req.body: {"description":"test","duration":"60","date":"1990-01-01"}
req.params: {"_id":"635333a3e114cbf0bd8f3141"}
req.query: {}
POST exercise
req.body: {"description":"test","duration":"60","date":"1990-01-03"}
req.params: {"_id":"635333a3e114cbf0bd8f3141"}
req.query: {}
{
  '': 'Already in the database',
  username: 'fcc_test_16663970910',
  description: 'test',
  duration: 60,
  date: 'Mon Jan 01 1990',
  _id: '635333a3e114cbf0bd8f3141'
}
GET logs
req.body: {}
req.params: {"_id":"635333a3e114cbf0bd8f3141"}
req.query: {"from":"1989-12-31","to":"1990-01-04"}
fromN: 19891231 toN: 19900104 limit: 9007199254740991
all exercises: [
  {
    "_id": "635333a3e114cbf0bd8f3144",
    "userid": "635333a3e114cbf0bd8f3141",
    "description": "test",
    "duration": 60,
    "date": 19900101,
    "__v": 0
  }
]
found exercises: [
  {
    "_id": "635333a3e114cbf0bd8f3144",
    "userid": "635333a3e114cbf0bd8f3141",
    "description": "test",
    "duration": 60,
    "date": 19900101,
    "__v": 0
  }
]
{
  _id: '635333a3e114cbf0bd8f3141',
  username: 'fcc_test_16663970910',
  count: 1,
  log: [ { description: 'test', duration: 60, date: 'Mon Jan 01 1990' } ]
}

You can see there are two entries posted and both fall in the requested log range of dates, but only one is returned. So at least part of the problem is in how you are constructing your log or how you are saving the entries. Careful examination of the logging output should illuminate the problem.

You also have timezone offset issues as mentioned previously because I don’t see any code to handle them. As you seem to be in UTC locally and the server is in UTC, you may not notice the issue (it’s failing for me now in US CDT). However, that won’t affect this test.

Thank you so much for your help Jeremy - by logging the POST requests and then looking at the exercises actually in the database I realised that I wasn’t posting the exercise for 1990-01-03 - I looked at my code and realised I was testing for already existing exercises by userid and description - I should have been testing for userid and description and duration and date - as soon as I changed that I passed all the tests!! :slight_smile:

Thank you again

How did you do it what did you change?

I fetched duration and date from req.body:

  const {description, duration, date} = req.body;

and in the findone i used:

  exercise.findOne({userid: id,
                    description: description,
                    duration: duration,
                    date: passedDate
                   })
1 Like