Tell us what’s happening:
Failing test for ‘Back End Development and APIs’
“The response returned from POST /api/users/:_id/exercises will be the user object with the exercise fields added.
do it.”
Response in my solution:
{
username: user.username,
_id: user._id,
exercises: [
{
description: exercise.description,
duration: exercise.duration,
date: exercise.date.toDateString()
}
]
}
Although I have tested using other ways but it kept failing. Help
###Your project link(s)
solution: http://localhost:3000
githubLink: http://localhost:3000
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Challenge Information:
Back End Development and APIs Projects - Exercise Tracker
Please post a repo with all your code.
exercise
isn’t a property, it is just the user object with the exercise properties added. Look at the example responses.
User:
{
username: "fcc_test",
_id: "5fb5853f734231456ccb3b05"
}
Exercise:
{
username: "fcc_test",
description: "test",
duration: 60,
date: "Mon Jan 01 1990",
_id: "5fb5853f734231456ccb3b05"
}
This is your exercise response:
{
"username": "fcc_test_17235062179",
"_id": "66ba9e29a6012f75f4830f90",
"exercise": {
"username": "fcc_test_17235062179",
"_id": "66ba9e29a6012f75f4830f90",
"description": "test",
"duration": 60,
"date": "1990-01-01T00:00:00.000Z",
"__v": 0
}
}
The exercise
you create already has all the properties on it. You can construct an exercise response object, remember to use .toDateString
for the dates in the response.
All the dates in the log objects are the same, which doesn’t seem right. I also believe some of them should have more than one object considering they are not yet filtered.
Can’t really look at it more right now.
Now updated the response, Still this test is failing
{
“username”: “nisha146”,
“_id”: “66baaed1e4a3e2449bf5e3f5”,
“description”: “test”,
“duration”: 40,
“date”: “Sun Aug 11 2024”
}
Please push your updated code so people can test it.
I have updated my file with the response
const exerciseResponse = {
username: exercise.username,
_id: exercise._id,
description: exercise.description,
duration: exercise.duration,
date: exercise.date.toDateString()
};
Your catch in the /api/users/:_id/exercises
route is throwing. Log out the error.
Duplicate key errors are usually related to unique schema values, but in this case, I think you have created an implicit/indirect unique value related to your exerciseSchema id. If you try using the form and create a user and then add two exercises to the same user you can see the same error on the second exercise submission.
You can change the _id
property in the exerciseSchema to something like userId
and then use that when creating a new exercise. But in the exercise response you send the user id _id: user._id
. Doing this I did manage to fix it so your code passed all the tests but I’m not sure if I have explained it correctly.
/* Schema */
const exerciseSchema = new mongoose.Schema({
userId: { type: mongoose.Schema.Types.ObjectId, ref: "User", required: true },
username: { type: String },
description: { type: String, required: true },
duration: { type: Number, required: true },
date: { type: Date, required: true },
});
/* /api/users/:_id/exercises route */
const exercise = new Exercise({
username: user.username,
userId: _id,
description,
duration,
date: date ? new Date(date) : new Date(),
});
await exercise.save();
const exerciseResponse = {
username: exercise.username,
_id: user._id,
description: exercise.description,
duration: exercise.duration,
date: exercise.date.toDateString(),
};
Updated index.js
Still I am not able to pass these tests:
The response returned from POST /api/users/:_id/exercises will be the user object with the exercise fields added.
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.
Your code is passing for me.
Are you not passing the tests now?
Yes, Only this test is failing.
I would suggest you delete your collection and try again.
As I said, your code is passing for me with my own DB.
If that doesn’t help.
Inspect the request/response in the browser dev tools on the network tab when you submit. See if you can figure out what part of the response isn’t as expected.
I have tried all possible solutions. Sadly, still no luck
I don’t know what that entails, what are “all possible solutions”.
Inspect the request/response and see what you are sending vs what is required.
As said, your code is passing for me.
Inspect the request/response when you submit, not manually testing it.