Back End Development and APIs Projects - Exercise Tracker

Tell us what’s happening:

Hi Community,

i’m sticking with this last exercise a few weeks now, also wrote the entire code from scratch again, but i want that be done really hard now, i’ts the last project in this course to finally get my degree :slight_smile: and i hate unfinished things.

Whats going on is a GET request and the response should deliver a object with correct properties fetched from a database (mongoose).

The exercise which fails says:
The date property of any object in the log array that is returned from GET /api/users/:id/logs should be a string. Use the dateString format of the Date API.

So, i build me a logger specially for that, it logs me
‘console.log(typeof exerciseObj.date, exerciseObj.date)’
and every date property in the date array HAVE type String - says the console.

I don’t know how to get over it now… Of course it’s something wrong in the code i wrote, but i cant find it.

Here is the code snippet for the task (removed all comments for readability, you can read them in the replit):

app.get(‘/api/users/:_id/logs’, function(req, res){

let userID = req.body[“_id”] || req.params._id,
from = req.query.from,
to = req.query.to,
limit = req.query.limit;

if (from) {
from = new Date(from);
from = from.toDateString();
if (from == “Invalid Date”) {
res.status(500).json(“Invalid ‘from’ Date”);
return;
};
};

if (to) {
to = new Date(to);
  to = to.toDateString();
if (to == "Invalid Date") {
  res.status(500).json("Invalid 'to' Date");
  return;
  };
};

if (limit) {
  limit = parseInt(limit);
  if (isNaN(limit)) {
    res.json("Invalid 'Limit'");
    return;
  };
};

User.findOne({ “_id” : userID }, (error, data) => {

if (error || !data) {
  res.status(500).json("Incorrect UserID");
  return
} else {
  
  const objToReturn = { "_id" : userID, "username" : data.username}, searchObj = { "username" : data.username }; dateSearchObj = {};

  if (from) {
      objToReturn.from = from;
    dateSearchObj["$gte"] = from;

    if (to) {
        objToReturn.to = to;   
      dateSearchObj["$lt"] = to;
    } else {
      const event = new Date();
      dateSearchObj["$lt"] = event.toDateString();
    };
  };

  if (to) {
    objToReturn.to = to;
    dateSearchObj["$lt"] = to;
    dateSearchObj["$gte"] = new Date("1970-01-01");
  };

  if (to || from) {
    searchObj.date = dateSearchObj;
  };

  Exercise.count(searchObj, (error, data) => {
    
    if (error || !data) {
      res.status(500).json("Invalid Date to search in exercise log");
      return
    };

    let exerciseCounter = data;
    
    if (limit) {
      if(limit < exerciseCounter){
          exerciseCounter = limit;
      };
    };

    objToReturn["count"] = exerciseCounter;
    
    Exercise.find(searchObj, (error, data) => {

      if (error || !data) return console.log(error);

      let logArray = [], exerciseObj = {}, counter = 0;

      data.forEach(function(item) {
        counter += 1;
        if (counter <= limit || !limit) {
          exerciseObj = {};
          exerciseObj.description = item.description;
          exerciseObj.duration = item.duration;
          exerciseObj.date = item.date.toDateString();
          **console.log(typeof exerciseObj.date, exerciseObj.date)**
          logArray.push(exerciseObj);
        };
      });
      
      objToReturn.log = logArray;
       res.status(200).json(objToReturn);
    });
  });
};

});
});

Thank you much for your time!
WKD,
greets from Vienna, Austria

Your project link(s)

solution: https://replit.com/@wkdminerva/fcc-exercise-wkd
Here is the code:
Invitation to collaborate on Replit - Replit

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Back End Development and APIs Projects - Exercise Tracker

Link to the challenge:

dateSearchObj[“$lt”] = to;

You can try using $lte operator instead of the $lt for the date filter.

EDIT ADD:

Also, these two fields in your code are not part of the expected output’s fields:

objToReturn.from = from;
objToReturn.to = to;

which is adding these to the log’s output:

  from: 'Sat Jul 23 2022',
  to: 'Thu Jul 28 2022',

The log’s output should have only these fields as shown below:

Log:

{
  username: "fcc_test",
  count: 1,
  _id: "5fb5853f734231456ccb3b05",
  log: [{
    description: "test",
    duration: 60,
    date: "Mon Jan 01 1990",
  }]
}

Another issue is that the console.log debugging code at this point in your code when you specify the from and to query parameters for the GET request:

console.log("searchObj.date:", searchObj.date);
Exercise.count(searchObj, (error, data) => { ...

prints this and that doesn’t match the from and to dates:

searchObj.date: { '$gte': 1970-01-01T00:00:00.000Z, '$lt': 'Thu Jul 28 2022' }

Thank you so much, prasadsaya!

I’m almost a litte bit angry to me that i didnt see that…^^
tunnel vision i would say.

Thanks & greets from Vienna

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