Tell us what’s happening:
I keep getting a TypeError: Cannot read properties of null (reading 'log')
for test case 7 when trying to pass the tests. The FCC test is also showing Test timed out. Any help is appreciated.
Here is my code for this exercise:
app.post('/api/users/:_id/exercises', async (req, res) => {
try {
const update = {
date: req.body.date ?
(new Date(req.body.date)).toDateString() :
(new Date()).toDateString(),
duration: parseInt(req.body.duration),
description: req.body.description
};
var Docs = await Exercise.findOne({_id: req.body[':_id']});
Docs.log.push(update);
await Docs.save();
console.log("checking for id: " + req.body[':_id']);
res.json({
username: Docs.username,
description: update.description,
duration: update.duration,
date: update.date,
_id: req.body[':_id']
});
} catch(err) {
return console.log(err);
}
})
###Your project link(s)
solution: https://jefische-boilerplatepro-o5ea24jj74o.ws-us117.gitpod.io/
githubLink: GitHub - jefische/boilerplate-project-exercisetracker: A boilerplate for a freeCodeCamp project.
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Challenge Information:
Back End Development and APIs Projects - Exercise Tracker
ILM
January 2, 2025, 9:25pm
2
the only log
I see is here, you should maybe check what value Docs
have with a console.log
When adding the console.log(Docs)
it returns null
.
I’m able to generate the correct response object in my local environment but am running into issues when trying to pass the test.
ILM
January 2, 2025, 9:33pm
4
where does Exercise
come from?
Here is my Mongoose schema:
const Schema = mongoose.Schema;
const ExerciseSchema = new Schema({
username: { type: String, required: true },
_id: { type: String, required: true },
count: Number,
log: [{description: String, duration: Number, date: Date}]
});
// Create a Model
let Exercise = mongoose.model("Exercise", ExerciseSchema);
ILM
January 2, 2025, 9:37pm
6
try to think on when Docs
could get a null
value (when findOne
would return null
), and is that expected? and you need to deal with it properly. Or is there something going wrong with the database?
I know if there is no id match findOne
returns null
which I would expect, but I’m not sure how to deal with it. I tried adding a conditional block but FCC still throws the Test time out.
app.post('/api/users/:_id/exercises', async (req, res) => {
try {
const update = {
date: req.body.date ?
(new Date(req.body.date)).toDateString() :
(new Date()).toDateString(),
duration: parseInt(req.body.duration),
description: req.body.description
};
var Docs = await Exercise.findOne({_id: req.body[':_id']});
if (Docs != null) {
Docs.log.push(update);
await Docs.save();
res.json({
username: Docs.username,
description: update.description,
duration: update.duration,
date: update.date,
_id: req.body[':_id']
});
}
} catch(err) {
return console.log(err);
}
})
ILM
January 2, 2025, 9:45pm
8
you are not giving a response if Docs
is null
, you must always give a response
ah okay, I added an else block and the test is passing now.
app.post('/api/users/:_id/exercises', async (req, res) => {
const update = {
date: req.body.date ?
(new Date(req.body.date)).toDateString() :
(new Date()).toDateString(),
duration: parseInt(req.body.duration),
description: req.body.description
};
var Docs = await Exercise.findOne({_id: req.body[':_id']});
if (Docs != null) {
Docs.log.push(update);
await Docs.save();
res.json({
username: Docs.username,
description: update.description,
duration: update.duration,
date: update.date,
_id: req.body[':_id']
});
} else {
res.send("No ID found");
}
})
I’m running into issues with test case 8 now, but will post again later if I can’t work through it. Thanks for the help!