Exercise Tracker: Trouble with Getting Full User Log

Tell us what’s happening:
I’m having trouble with the GET to api/users/:_id/logs.
The UserLog object that I return contains a count which is Number, a log which is an array of exercises where each excercise is an object with a description (String), duration (Number), and a date (String).
When I try to run the tests, I fail all the ones for GET api/users/:_id/logs and I cannot figure out why. (I’m not concerned with the from, to, limit params part yet).

Here’s my UserLog Schema

const userLogSchema = new Schema({
  username: { type: String, required: true },
  count: { type: Number, required: true, default: 0 },
  log: [{
    description: { type: String, required: true },
    duration: { type: Number, required: true },
    date: { type: String, required: true, default: new Date().toDateString() }
module.exports = { 
  UserLog: mongoose.model("UserLog", userLogSchema),

Finding full exercise log

//get a user log (single)
router.get('/users/:_id/logs', (req, res) => {
  UserLog.findById(req.params['_id'], (err, userLog) => {
    if (err) return res.json({ message: "ERROR finding userlog" })

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36

Challenge: Exercise Tracker

Link to the challenge:

It’s your date processing on the add exercise route when there is no date specified. Here’s some of your logging with some modifications:

Request! POST /api/users - ::ffff:
RECIEVED! new user: fcc_test_16332730329
New user created! {"count":0,"_id":"6159c4c810eb5f02dd0204a5","username":"fcc_test_16332730329","log":[],"__v":0}
Request! POST /api/users/6159c4c810eb5f02dd0204a5/exercises - ::ffff:
RECEIVED! new exercise [Object: null prototype] { description: 'test', duration: '60' }
invalid date
Request! GET /api/users/6159c4c810eb5f02dd0204a5/logs - ::ffff:
generating user log
{ _id: '6159c4c810eb5f02dd0204a5' }

You can see you are receiving a valid exercise request with no date, but the route is flagging it as invalid. The log entry later confirms this as there are no entries in the log. It’s very confusing since later on it appears to work, but it’s always with exercise objects that have dates.

You can fix the problem by simplifying the date processing. Just let Date() handle the parsing like in the timestamp project since it can flag invalid dates and then you can just test if there is a date present to decide whether to use now.

Ironically enough, the from/to and limit tests all use dates on their exercises, so you probably would have passed those had they been implemented.

1 Like

Wow thanks for that. For some reason when I enter an exercise with no date in the form, it passes an empty string ('') into req.body.date, so thats what I tested for. But the tests pass req.body.date as undefined. That was a fun time figuring out, but now I pass all the the GET requests to api/users/:id/logs. Now to do the last test!

Here’s my fix:

const date = (req.body.date === '' || req.body.date === undefined) ? new Date() : new Date(req.body.date)
let dateString = date.toDateString()

Thanks again for your time, I didn’t expect an answer so quickly:)

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