Trouble with Exercise Tracker Project - User Story 4

Hi fellow Campers! I’m having some trouble with the Exercise Tracker project in the APIs and Microservices section. I successfully completed the first 3 projects fairly easily, however I can’t figure out why I’m failing the fourth user story on this one.

The user story is:

" You can POST to /api/users/:_id/exercises with form data description , duration , and optionally date . If no date is supplied, the current date will be used. The response returned will be the user object with the exercise fields added."

When manually viewing my api/users output, I can see the freecodecamp test entries including the logs that were added to the exercises array, so I know they are POSTing ok. Could it have something to do with the formatting of my response to the POST request?

My test output for the POST looks like the following:

{“username”:“test”,"_id":“8d56ea07e4ac6175807ed5f66279715d394d8885”,“exercises”:[{“description”:“test”,“duration”:“60”,“date”:“Tue Jun 15 2021”},{“description”:“test2”,“duration”:“30”,“date”:“Tue Jun 15 2021”}]}

I am returning the user object, including the list of exercises submitted. I included sample output from my api/users request as a json file in the root directory of my project (which you can access below). Has anyone completed this project recently or could anyone point me in the right direction on what I’m missing/doing wrong here? Thanks in advance for the help.

My project link

solution: https://replit.com/@robinroswell/boilerplate-project-exercisetracker

My browser information:

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

Challenge: Exercise Tracker

Link to the challenge:

I made the same mistake. You actually need to return only the description, duration, and date of the newly added exercise, not a list of exercises.

        {
        _id:  . . . ,
        username: ...,
        description: ...,
        duration: ...,
        date: ...
        }
2 Likes

Thanks for the reply @twotani ! I’m still having issues when returning the exercise data in the format you suggested though. I’m returning the user id and username along with the exercise data that was submitted in the POST request.

Could someone possibly give me a basic outline of how they are structuring the data on this project? I have a user array containing the user objects formatted like so:

[
{
_id: "sampleID",
username: "sampleUsername"
},
{
_id: "anotherSampleID",
username: "anotherSampleUsername"
}
]

Additionally, I have an exercises array formatted like this:

[
{
_id: "sampleID",
username: "sampleUsername",
description: "sampleDescription",
duration: "30",
date: "Wed Jun 16 2021"
},
{
_id: "anotherSampleID",
username: "anotherSampleUsername",
description: "anotherSampleDescription",
duration: "60",
date: "Wed Jun 16 2021"
}
]

Here are the outputs of the freeCodeCamp POST tests as an example:

{
  _id: 'bedc8f6c0a0f00decf81a03e1e4ddfd5a5d808ea',
  username: 'fcc_test_16238555321',
  description: 'test',
  duration: '60',
  date: 'Mon Jan 01 1990'
}

{
  _id: '4b7ee8ac52b7be0eb845562c3936138514c31445',
  username: 'fcc_test_16238555322',
  description: 'test',
  duration: '60',
  date: 'Wed Jun 16 2021'
}

{
  _id: '209e5722e3ea9180dba97be54148a4315f4f16cc',
  username: 'fcc_test_16238555324',
  description: 'test',
  duration: '60',
  date: 'Wed Jun 16 2021'
}

{
  _id: '518632e58428eb605c2748b377fb2706a4b5b9cf',
  username: 'fcc_test_16238555325',
  description: 'test',
  duration: '60',
  date: 'Mon Jan 01 1990'
}

{
  _id: '518632e58428eb605c2748b377fb2706a4b5b9cf',
  username: 'fcc_test_16238555325',
  description: 'test',
  duration: '60',
  date: 'Tue Jan 02 1990'
}

Thanks for the help, I have identified the issue. The duration property in the response object must apparently be an integer. I didn’t see that specified in the user story or project details.

parseInt(duration)

Hey,

Just curious: Why aren’t you using MongoDB/Mongoose?

1 Like

OMG. :grimacing:

Apparently I skipped that section by mistake! Haha that might be why I’m having trouble. Thanks for asking that or I might never have known!

1 Like

Learning the hard way huh? :grinning_face_with_smiling_eyes: and got pretty far I should say :+1:

2 Likes

I’m actually having the same issue and I’m not sure why. I’m pretty sure that I’m returning the correct information in the right format, maybe I’m missing something little. Here is my code for this endpoint though:

app.post("/api/users/:_id/exercises",async(req,res) => {
const shortMonths= [“Jan”,“Feb”,“Mar”,“Apr”,“May”,“Jun”,“Jul”,“Aug”,“Sep”,“Oct”,“Nov”,“Dec”];
const shortDays= [“Sun”,“Mon”,“Tue”,“Wed”,“Thu”,“Fri”,“Sat”];
if (!req.body.date) { //if date is null
console.log(‘null!’);
const newDayString= new Date(); //set the date to a new date
const year= newDayString.getUTCFullYear();
const month= newDayString.getUTCMonth();
const day= newDayString.getUTCDay();
const getDate= newDayString.getUTCDate();
const shortDate= shortDays[day]+" “+shortMonths[month]+” “+getDate+” "+year;
console.log("short date: “+shortDate);
req.body.date=shortDate;
}else{
const newDayString= new Date(req.body.date);
const year= newDayString.getUTCFullYear();
const month= newDayString.getUTCMonth();
const day= newDayString.getUTCDay();
const getDate= newDayString.getUTCDate();
const shortDate= shortDays[day]+” “+shortMonths[month]+” “+getDate+” "+year;
console.log("short date: "+shortDate);
req.body.date=shortDate;
}
const exercises= new Exercise(req.body);
User.findById(req.params._id,async(err,user) => {
try {
user.log.unshift(exercises);
console.log(user);
const {username,_id}=user;
const {description,duration,date}=user.log[0];
user.save();
res.send({_id,username,description,duration:parseInt(duration),date});
}catch(e) {
res.send({e});
}
});
});

I tried again, but I’m still getting the same result even with my rework. Even though I’m doing it a slightly different and in some ways better way I feel like I’m still sending back the same result and it’s still not working. I’m not sure what I’m doing wrong here. Here is my updated code.

app.post("/api/users/:_id/exercises",async(req,res) => {
if (!req.body.date) { //if date is null
console.log(‘null!’);
cont newDayString= new Date().toISOString().substring(0,10); //set the date to a new date
req.body.date=newDayString;
}else{
const newDayString= new Date(req.body.date).toISOString().substring(0,10);
req.body.date=newDayString;
}
const exercises= new Exercise(req.body);
console.log(exercises);
await User.findByIdAndUpdate(req.params._id,{
$push:{
log:exercises
}
},{new:true,useFindAndModify: false},(err,user) => {
try {
console.log(user);
const {username,_id}=user;
const {description,duration,date}=user.log[0];
const modDate= new Date(date).toDateString();
user.save();
res.send({_id,username,modDate,description,duration});
}catch(e) {
res.send({e});
}
});
});

Never mind, I realized I was returning modDate in res.send(). I fixed this and passed user story #4 now. Finally!

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