I’m trying to get a better sense for why my GET request isn’t working properly on my exercise tracker. When trying to GET logs the date isn’t converted into a DateString (I know why (it only gets turned into a date string in my POST with exercises), but not sure how to fix it yet), and currently there’s an extra unwanted id output in the exercise log. Any feedback would be appreciated. Here’s my repo and here are the three routes which are relevant for my question:
app.get("/api/users/:_id/logs?", function (req, res) {
const { _id } = req.params;
return User.findById(_id, (err, userRecord) => {
if (err) {
console.log(err);
} else {
res.json({
_id: _id,
username: userRecord.username,
count: userRecord.log.length,
log: userRecord.log,
});
}
});
});
app.post("/api/users", async function (req, res) {
let nameToString = req.body.username.toString();
let hash = crypto.randomBytes(20).toString("hex");
const newUser = new User({
username: nameToString,
_id: hash,
log: [],
});
const userRecord = await newUser.save();
res.json({
_id: userRecord._id,
username: userRecord.username,
});
});
app.post("/api/users/:_id/exercises", async function (req, res) {
const { _id } = req.params;
const { date, duration, description } = req.body;
function makeDate(string) {
if (!date) {
return new Date().toDateString();
} else {
return new Date(string).toDateString();
}
}
User.findById(_id, async (err, userRecord) => {
if (err) {
console.log(err);
} else {
await userRecord.log.push({ date, duration, description });
await userRecord.save();
res.json({
_id: _id,
username: userRecord.username,
date: makeDate(date),
duration: duration,
description: description,
});
}
});
});```
I’m not sure I understand what your question is.
You should log your response from the GET log route and inspect it. You really should log all the route inputs and responses while running the tests to debug anyway. That should help.
Post a repl so that we can easily fork and run your code for further assistance.
My first question is what changes do I need to make (or any hints/ideas) to get the dateString to always print as a proper date string? Secondly I see in Postman when I make requests and check the responses I’m getting an erroneous id field within the log of my GET request for logs. What parts of my code should I look at to alleviate that?
Here’s a repl of my project, thanks: exercisetracker - Node.js Repl - Replit
I adding logging to your routes to illustrate the problem:
POST exercise
req.body: {"description":"test","duration":"60"}
req.params: {"_id":"7b2d3cb9ec156274e4bcb887e0388adaa437a5a5"}
req.query: {}
user record:
{
_id: '7b2d3cb9ec156274e4bcb887e0388adaa437a5a5',
username: 'fcc_test_16667383445',
log: [
{
description: 'test',
duration: 60,
_id: new ObjectId("635868a8c0b364a5a2829998")
}
],
__v: 1
}
response
{
_id: '7b2d3cb9ec156274e4bcb887e0388adaa437a5a5',
username: 'fcc_test_16667383445',
date: 'Tue Oct 25 2022',
duration: '60',
description: 'test'
}
GET logs
req.body: {}
req.params: {"_id":"7b2d3cb9ec156274e4bcb887e0388adaa437a5a5"}
req.query: {}
{
_id: '7b2d3cb9ec156274e4bcb887e0388adaa437a5a5',
username: 'fcc_test_16667383445',
count: 1,
log: [
{
description: 'test',
duration: 60,
_id: new ObjectId("635868a8c0b364a5a2829998")
}
]
}
As you can see, your code does not handle missing dates. Once you handle the dates correctly, I suppose you can store them as date strings in POST exercise or loop over the exercises and process them in the GET log route.
The _id
in the log is the MongoDB ID of the exercise record. They aren’t incorrect, just extraneous. You can filter them out in find()
calls or you can loop over the log items in GET log and not include them.
1 Like
How did you get that logging? I’m trying to add that myself, thanks a bunch for your response/help
At the beginning of each route
console.log('POST exercise');
console.log(`req.body: ${JSON.stringify(req.body)}`);
console.log(`req.params: ${JSON.stringify(req.params)}`);
console.log(`req.query: ${JSON.stringify(req.query)}`);
and before each return res.json(obj);
console.log(JSON.stringify(obj));
and likewise with other console.log()
statements at other points as necessary.
1 Like