Can't pass Exercise Tracker, error: the date property should be a string

Tell us what’s happening:
Describe your issue in detail here.
can’t pass Exercise Tracker, error: the date property should be a string
but actually the date property is string, I don’t know shy I just can’t pass

Your project link(s)
my code is as below:

// post api, add a new exercise record to a given user
app.post('/api/users/:_id/exercises',(req,res) => {
  const {_id} = req.params;
  const {description, duration} = req.body;
  const date = req.body.date == ''?new Date().toDateString():new Date(req.body.date).toDateString();
  let newExercise = {description,duration:parseInt(duration),date}

  userExercise.findOneAndUpdate(
    {_id:_id},
    { $push: {log:newExercise} },
    {returnOriginal:false,useFindAndModify:false},
     (err,result) => {
       if(err) return console.error(err);
       res.json({username:result.user,description,duration:parseInt(duration),date,_id:result._id})
     }
   );
})

// GET user's exercise log: GET /api/users/:_id/logs?[from][&to][&limit]
// [ ] = optional
// from, to = dates (yyyy-mm-dd); limit = number 
app.get('/api/users/:_id/logs', (req,res) => {
  let {_id} = req.params;
  let {from,to,limit} = req.query;
  userExercise.findById(_id,(err,result) => {
    if(err) return console.error(err);
    let filteredExercise = result.log;
    if(from)
     filteredExercise = filteredExercise.filter(e => new Date(e.date) > new Date(from) );
    if(to)
     filteredExercise = filteredExercise.filter(e => new Date(e.date) < new Date(to) );

    if(parseInt(limit) > 0){
       filteredExercise = filteredExercise.slice(0,parseInt(limit));
    }
    let logs = []
    filteredExercise.forEach(item => {
      const {description,duration,date} = item
      logs.push({description,duration,date,})
    })
   res.json({username:result.user,count:filteredExercise.length,_id:result._id,log:logs});
 
  })
})

and the response return is as below:

Could anyone help me?

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36

Challenge: Exercise Tracker

Link to the challenge:

I realize that fcc test the date with an undefined type, not only empty string bro! This is sick. Just use the code below and go to sleep :frowning:
let date = req.body.date === ‘’ || req.body.date === undefined ? new Date() : new Date(req.body.date)

2 Likes

Thanks for your reply, but I still failed to pass :rofl:

your solution helped me get rid of undefined dates, but it still won’t let me pass, and give me this error: the date property should be a string
but actually the date property is string again, no clue why

I am having the same problem, I have reviewed the code in detail and I have compared results and everything seems to be fine but the test continues to fail :frowning:

Actually what I did was try to get the output to return in the correct log format that fcc gave, then I used .toDateString() as fcc suggested. When I had this problem, I console.log every test case they used, data type, content, return data from my code and I was really lucky to be able to pass before going to sleep :frowning:

Thank you very, very much! I was blocked on the same error for days and now it’s ok!!

1 Like

Thanks I passed finally :grinning: I console.log every test case as you suggested

1 Like

I think the problem is related to the date of the testing server and the date where your server is hosted. This morning I decided to do the test again with the same code and I managed to pass it (at that moment I did not send it) and I just tried again and the date test failed again

2 Likes

Thank you! I had to hardcode yesterday’s date in order to pass test #15. I think the problem is related to timezone offset.

1 Like

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