Back End Development and APIs Projects - Exercise Tracker

Tell us what’s happening:
Describe your issue in detail here.
Someone guide me where i have missed a hint because my second last test isn’t passing what could be the problem and how can i go about it?
Thanks in advance.

Your project link(s)

solution: boilerplate-project-exercisetracker-1 - Replit

Your browser information:

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

Challenge: Back End Development and APIs Projects - Exercise Tracker

Link to the challenge:

You should construct the date correctly when creating an exercise that way you do not have to do anything to the res objects for the date. Save the date to the DB in the correct format.

Just as an aside, new Date(req.body.date).toDateString() in the log route is returning Invalid Date.

Request URL: https://boilerplate-project-exercisetracker-1.emmanuelonyanga.repl.co/api/users/62e14a69254f1a001c58c788/logs

Response:

{"_id":"62e14a69254f1a001c58c788","username":"fcc_test_16589318176","count":1,"date":"Invalid Date","log":[{"_id":"62e14a6a254f1a001c58c78a","id":"62e14a69254f1a001c58c788","description":"test","duration":60,"date":"2022-07-27T14:23:38.015Z","__v":0}]}

Both date property values are wrong as you can see.

Here is the screenshot of the console


i have tried my level best with one date parameter and it’s fine now but am stuck with the other date parameter,please am humbly waiting for your guidance as i will be very grateful if you did so.

As I said, if you just save the dates in the correct format to begin with you do not have to do anything.

Example code.

const date = dateFromParam ? new Date(dateFromParam).toDateString() : new Date().toDateString()

Otherwise, you will have to loop the log array you get back and transform the date property by passing each of the log date strings you have now to a date constructor.

Partial pseudo-ish code.

{ ...props, date: new Date(theWrongDateString).toDateString() }

Just being sincere with you, am more confused and stuck the more, with what you are saying
kindly breakdown and guide as i will be very grateful.

In your code here:

const exercises = new Exercises({
  id: req.params._id, 
  description: req.body.description, 
  duration: parseInt(req.body.duration), 
  date: isNaN(new Date(req.body.date))?new Date():new Date(req.body.date)
});

Create the date in the correct format, as I have already shown. Use .toDateString() on the date you are constructing.


Or loop the log array and transform it.

I really can’t explain this better than I have without posting spoiler-ish code. I will use the response object I posted before for the example.

const data = {
  _id: '62e14a69254f1a001c58c788',
  username: 'fcc_test_16589318176',
  count: 1,
  date: 'Invalid Date',
  log: [
    {
      _id: '62e14a6a254f1a001c58c78a',
      id: '62e14a69254f1a001c58c788',
      description: 'test',
      duration: 60,
      date: '2022-07-27T14:23:38.015Z',
      __v: 0,
    },
  ],
};

const transformedLogs = data.log.map((logObj) => {
  return {
    ...logObj,
    date: new Date(logObj.date).toDateString(),
  };
});

console.log(transformedLogs);

/* 
[ { _id: '62e14a6a254f1a001c58c78a',
    id: '62e14a69254f1a001c58c788',
    description: 'test',
    duration: 60,
    date: 'Wed Jul 27 2022',
    __v: 0 } ]
*/

It obviously shouldn’t have double ids but that is just how that response looked.

Am still stuck and i have tried to change the code as per how you instructed but nothing seems to pass at all.
Waiting

I don’t see where you have done anything I suggest you do in your code.

But I do think I have to make a correction with my map example because if you just spread all the props, I think you will get a bunch of properties you do not want on the log objects. So you have to return a specific object with just the three properties that are the requirement for the log objects.

So inside your res.json for the log property, map data and inside the map callback return a new object with the description, duration, and date. The date needs to be formatted using toDateString. When I do that your code passes.

The shape is something like this:

res.json({
  otherProps,
  someProp: someArray.map((someObject) => ({
    prop1: someObject.prop1,
    prop2: someObject.prop2,
    prop3: someObject.prop3.toDateString(),
  }))
})

@lasjorg am speechless, thank you very much for the guidance Be Blessed wherever you are though it has been really hard to pass this stage.

Nice, good to hear you got it. I tried not to spoil it too much but code can be a little hard to explain in words at times.

The struggle is good, that is how we learn. Keep it up and happy coding.

Welcome :clap: :clap: :pray: :pray: :handshake: :handshake:
Actually i was stuck on this very one only, the rest i had gone through,good enough i learnt a lot in this one specifically.