Exercise Tracker: Can't Pass the last 7th test

It works fine when I try it out on the db here is the format of json response I am getting. The last test case is still failing, what could be the reason?

Link for project: https://replit.com/@PushkarMahajan/boilerplate-project-exercisetracker-5#server.js

{"_id":"61270a9fc3c23da04e8f9c68","username":"fcc_test_16299485731","from":"Mon Dec 31 1990","to":"Wed Jan 03 1990","count":2,"log":[{"description":"test","duration":60,"date":"Mon Jan 01 1990"},{"description":"test","duration":60,"date":"Tue Jan 02 1990"}]}
app.get("/api/users/:_id/logs", (req, res) => {
  const {from , to } = req.query;
  if(from == undefined)
  {
     UserModel.find({ _id: req.params._id}, (err, found) => {   
    let tempLog = [];
    found[0].log.map((element) => {
      tempLog.push({
        description: element.description,
        duration: element.duration,
        date: element.date.toDateString()
      });
    })
    res.json({
      _id: req.params._id, 
      username: found[0].username,
      count: parseFloat(found[0].log.length),
      log: tempLog
    });
  })
  }
  else{
    let tempDate = [];
    console.log(req.query);
    UserModel.findOne({ _id: req.params._id}, (err, found) => {
      let resultLog = found.log;
      if(req.query.from)
      {
        const fromDate = new Date(from);
         resultLog=found.log.filter((exec) => exec.date >= fromDate)
      }
      
      if(req.query.to)
      {
        const toDate = new Date(to);
        resultLog = found.log.filter((exec) => exec.date <= toDate)
      }
     
      if(req.query.limit)
      {
        resultLog=resultLog.splice(0, req.query.limit)
      }
      let finalLog = [];
      resultLog.map((ele) => {
        finalLog.push({
          description: ele.description,
          duration: ele.duration,
          date: ele.date.toDateString()
        });
      })
      const fromDate = new Date(from).toDateString();
      const toDate = new Date(to).toDateString();
      res.json({
              _id: req.params._id, 
              username: found.username,
              from: fromDate,
              to: toDate,
              count: parseFloat(finalLog.length), 
              log: finalLog
      })
    })
  } 
})

Always console.log() your inputs and responses when you are debugging a route. When I forked your project, set up the logging, and ran the fCC tests, I got:

req.query.from: undefined
req.query.to: undefined
req.query.limit: 1
{
  _id: '61281acdfecb4946a2955483',
  username: 'fcc_test_16300182538',
  count: 2,
  log: [
    { description: 'test', duration: 60, date: 'Mon Jan 01 1990' },
    { description: 'test', duration: 60, date: 'Tue Jan 02 1990' }
  ]
}

The limit was supposed to be 1, but you sent 2 records.

Also, everywhere you have a response (like res.json()), you need to return the response (like return res.json()) or you will eventually get unexpected behavior.

Omg setting limit was the problem, Thankyou so much!!!

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