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 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: