My code keeps failing when in fact I believe it shouldn’t.
Here is my project link: https://replit.com/@theodore1250/boilerplate-project-exercisetracker
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36
Challenge: Exercise Tracker
Link to the challenge:
I didn’t look much at the code but I did look in the network tab when submitting, here is one of the responses.
{"error":"Log validation failed: date: Cast to date failed for value \"Invalid Date\" (type string) at path \"date\""}
I think the logic for what will happen if no date is passed is wrong.
new Date(req.body.date).toDateString() || new Date()
new Date(undefined).toDateString() || new Date()
// 'Invalid Date'
I didn’t actually test it.
@lasjorg
Thanks for your reply!
I have changed it from new Date() to Date.now().
Yet the last 7 tests keep failing.
My point is if req.body.date
is undefined your OR ||
logic doesn’t do what you want it to do.
new Date(undefined).toDateString()
does not evaluate to a falsy value, it evaluates to the string ‘Invalid Date’.
Yea yea! That’s true. Thanks
I have now used the ternary operator.
req.body.date ? new Date(req.body.date).toDateString() : new Date().
Now I only have two tests failing:
// running tests
The description property of any object in the log array that is returned from GET /api/users/:id/logs should be a string. (Test timed out)
You can add from, to and limit parameters to a GET /api/users/:_id/logs request to retrieve part of the log of any user. from and to are dates in yyyy-mm-dd format. limit is an integer of how many logs to send back.
// tests completed
Now I only have this test failing:
You can add from, to and limit parameters to a GET /api/users/:_id/logs request to retrieve part of the log of any user. from and to are dates in yyyy-mm-dd format. limit is an integer of how many logs to send back.
// tests completed
here is my code:
app.get("/api/users/:id/logs", async (req, res) => {
const user = await User.findById(req.params.id);
if (req.query.from && req.query.to && req.query.limit) {
console.log("with query");
const log = await Log.find({
date: { $gte: new Date(req.query.from), $lte: new Date(req.query.to) },
}).select("-_id -userid -__v");
let limitedLog = log.slice(0, Number(req.query.limit));
let userLog = limitedLog.map((each) => {
return {
description: each.description,
duration: each.duration,
date: new Date(each.date).toDateString(),
};
});
res.json({
_id: req.params.id,
username: user.username,
count: log.length,
log: userLog,
});
} else {
const log = await Log.find({ userid: req.params.id }).select(
"-_id -userid"
);
const count = log.length;
let userLog = log.map((each) => {
return {
description: each.description,
duration: each.duration,
date: new Date(each.date).toDateString(),
};
});
res.json({
_id: req.params.id,
username: user.username,
count: count,
log: userLog,
});
}
});
Is anything wrong with my code?
Those three parameters are optional so you should be able to only put limit=1 only without the other two variables I think this line preventing that
if (req.query.from && req.query.to && req.query.limit)
When initializing the variables you can do something like this
const limit = Number(req.query.limit) || 0
const from = req.query.from || new Date(0)
const log = await Log.find({
date: { $gte: from, $lte: to },
}).select("-_id -userid -__v").limit(limit);
Thank youuuuu!!!
The test has passed!
I did this, just as you said:
const limit = Number(req.query.limit) || 0;
const from = req.query.from || new Date(0);
const to = req.query.to || new Date(Date.now())
console.log("with query");
const log = await Log.find({
userid: req.params.id,
date: { $gte: from , $lte: to }
})
.select("-_id -userid -__v")
.limit(limit)
1 Like
system
Closed
April 14, 2022, 5:49am
9
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.