Exercise Tracker Code Optimization for final test Time Out prevention

Tell us what’s happening:
I’m currently having a problem finishing the Exercise Tracker’s Final Test in the Backend Development and API certification due to Time Out. I mainly used Replit and saved my code on the GitHub repository.

However, when I tried to clone the GitHub repo to my local device, I managed to pass all the tests without any problems.

Since I’ve seen some other people have solved the challenge with the replit server, I’m thinking about optimizing my replit code so that it passes all the tests much quicker, but I don’t know what kind of things that I can optimize.
I’ll paste the part of the code that makes me stuck in replit for easier troubleshooting.

function logging(obj){
  return {description: obj.description, duration: obj.duration, date: obj.date.toDateString()};
}
app.get('/api/users/:_id/logs/', (req, res) =>{
  if(Boolean(req.query.from || req.query.to || req.query.limit)){
    if(!Boolean(req.query.limit)){
      req.query.limit = Number.MAX_VALUE;
    }
    if(!Boolean(req.query.from)){
      req.query.from = '1970-01-01';
    }
    if(!Boolean(req.query.to)){
      req.query.to = '2050-12-31';
    }
    Exercise.find(
      {
        user_id: req.params._id,
        date: {$gte: req.query.from, $lte: req.query.to}
      }
    ).lean().limit(req.query.limit).exec((err,data) => {
      if(err){
        return res.send(err.message);
      }
      else if (data.length == 0){
        return res.send("Data empty");
      }
      else{
        return res.json(
          { username: data[0]['username'],
            count: data.length,
            _id: data[0]['user_id'],
            log: data.map(logging)
          });              
      }
    });
  } 
  else{
      Exercise.find({user_id: req.params._id}).lean().exec((err, data) =>{
        if(err){
          return res.send(err.message);
        }
        else if (data.length == 0){
          return res.send("Data empty");
        }
        else{
        return res.json(
          { username: data[0]['username'],
            count: data.length,
            _id: data[0]['user_id'],
            log: data.map(logging)
          }); 
          
        }
      });    
  }
});

Any kind of help and assistance is highly appreciated :smiley:

Your project link(s)

Run on replit:https://exercisetracker-api-project.danstep65.repl.co
Github: GitHub - DanStep65/exercisetracker-api-project: A repository for Exercise Tracker Project from freeCodeCamp. Made as a requirement to complete Backend Development and API Certification from FreeCodeCamp.
Your browser information:

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

Challenge: Exercise Tracker

Link to the challenge:

1 Like

I am also facing same problem

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