Tell us what’s happening:
I’m tearing my hair out with these two tests, and it seems like this is a really common struggling point for the challenge.
I’m properly managing the date format when it’s not included (param.date as well as today’s date if null are being stored as date objects), then I use .toDateString() and am able to convert to a string for the return output.
Test number 8:
The response returned from api/users/:id/exercises will be the user object with the exercise fields added.
I’ve written the challenge with two different sets of Schemas, currently doing an “all in one” Schema:
let userSchema = new Schema({
username: {
required: true,
type: String
},
log: [{
description: String,
duration: Number,
date: Date
}]
});
And am able to log exercises as expected. My return result from the POST is this, which to me looks like it meets the criteria, but it isn’t passing.
{
username: 'fcc_test_16748829723',
_id: new ObjectId("63d4af9c2ba075d08fe744ca"),
description: 'test',
duration: 60,
date: 'Sat Jan 28 2023'
}
I’m very confused if the key value pair I’m getting after saving my user object:
_id: new ObjectId("63d4af9c2ba075d08fe744ca")
is identical to this format which seems like what I usually see:
_id: "63d4af9c2ba075d08fe744ca"
And I wonder if that’s causing any issues.
Test number 15 I’m also not passing seems similar, but in this case, for the life of me I can’t cast the date obect to a string, even though I have tried a variety of toString()
functions including toDateString()
and toISODateString
This is my returned JSON for the api/users/:id/logs, and on screen it looks fine but again not passing. The date is being returned in what seems like an object, and attempt to convert to a string is below, using ‘map’ :
exerciselog: {
username: 'fcc_test_16748829723',
_id: new ObjectId("63d4af9c2ba075d08fe744ca"),
count: 1,
log: [
{
description: 'test',
duration: 60,
date: 2023-01-28T05:16:12.622Z,
_id: new ObjectId("63d4af9c2ba075d08fe744cc")
}
]
}
I am using Model.findOneAndUpdate like so:
User.findByIdAndUpdate({ _id: userID }, { $push: { log: newExercise } }, { new: true }, function(err, user) {
// error handling
// create return object with dates to strings (but isn't actually converting)
// return exercise log
}
Here is how I’m handling the exercise log to try and convert to strings before returning:
{ description: 'test', duration: 60, date: 1990-01-01T00:00:00.000Z }
workingLog.map(item => item.date.toDateString())
let exerciseLog = {
"username": user.username,
"_id": user._id,
"count": workingLog.length,
"log": workingLog
}
Code source is below. Thank you in advance for any input, this is driving me crazy!
Your project link(s)
solution: boilerplate-project-exercisetracker - Replit
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36
Challenge: Back End Development and APIs Projects - Exercise Tracker
Link to the challenge: