Back End Development and APIs Projects - Exercise Tracker

Tell us what’s happening:

I am failing to pass test 10 to test 15 for the exercise-tracker. I have tried everything. The problem seems to be on the ‘/api/users/:_id/logs’ endpoint.
My response contains all required fields that.


app.get('/api/users/:_id/logs', async (req, res) => {
  const { _id } = req.params;
  let { from, to, limit } = req.query ? req.query : null;

  const dateFormat = /^(19|20)\d\d-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/

  if (!from || !dateFormat.test(from)) {
    from = new Date('1970-01-01');
  } else {
    from = new Date(from);
  }

  if (!to || !dateFormat.test(to)) {
    to = new Date('2050-01-01');
  } else {
    to = new Date(to);
  }

 // Validate 'limit' and convert to an integer if valid
 limit = parseInt(limit, 10);
 if (isNaN(limit) || limit <= 0) {
   limit = 0; // No limit if 'limit' is missing or invalid
 } 

 try {
    const user = await queries.findAllExerciseById(_id, from, to, limit);  
    return res.json({ 
      username: user.username, 
      count: user.count,
      _id: user._id, 
      log: user.log
    });
 } catch (err) {
  console.log(err)
  return err;
 }
});

This is the code for the log endpoint.

Output from log endpoint during tests

{
    "username": "fcc_test_17203453187",
    "count": 2,
    "_id": "668a62e7902d6c75ceab12b3",
    "log": [
        {
            "description": "test",
            "duration": 60,
            "date": "Mon Jan 01 1990"
        },
        {
            "description": "test",
            "duration": 60,
            "date": "Wed Jan 03 1990"
        }
    ]
}

Test outputs.
// running tests
A request to a user's log GET /api/users/:_id/logs returns a user object with a count property representing the number of exercises that belong to that user.
A GET request to /api/users/:_id/logs will return the user object with a log array of all the exercises added.
Each item in the log array that is returned from GET /api/users/:_id/logs is an object that should have a description, duration, and date properties.
The description property of any object in the log array that is returned from GET /api/users/:_id/logs should be a string.
The duration property of any object in the log array that is returned from GET /api/users/:_id/logs should be a number.
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.
// tests completed
// console output
[TypeError: Cannot read properties of undefined (reading 'description')]
[TypeError: Cannot read properties of undefined (reading 'duration')]
[TypeError: Cannot read properties of undefined (reading 'date')]

I can’t seem to see where the TypeError is coming from because I can’t find any error messages in my app.

Finnally this is the query for retrieving all exercises

queries.findAllExerciseById = async (_id, from, to, limit) => {
    try {
        const user = await queries.findOneById(_id);
    
        if (!user) {
            throw new Error('User not found');
        };

        const logs = await exercise.find(
            { 
                id: _id, 
                date : {
                    $gte: from,
                    $lte: to
                }
            }, 
            {
                _id: 0, id: 0, __v: 0, 
            }
        ).limit(limit ? limit : null);
        
        const results = {
            _id: user._id.toString(),
            username: user.username,
            count: logs.length,
            log: logs.map(log => ({
                ...log.toObject(),
                date: log.date.toDateString(),
                duration: Number(log.duration)
            })) 
        }
        return logs ? results : null 
    } catch (err) {
        console.log('error finding user: ', err)
        return { error: 'Internal Server Error' };
    }
    
}

Code links
###Your project link(s)

solution: fcc-exercisetracker - Replit

Your browser information:

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

Challenge Information:

Back End Development and APIs Projects - Exercise Tracker

  1. The log array is empty in all your log responses, except for the last ones with a query.

  2. If we backtrack the responses, we can see your exercises responses with an optional date are all missing the date. Check you are using the correct date in the /api/users/:_id/exercises route (hint: you are not).

  3. This date error when creating exercises is what is causing your log array to be empty.

1 Like

Thank you so much. That was super helpful. I was passing date to the log query instead of formmattedDate. A very costly mistake on my part. All the tests passed seemlessly after I fixed it.

1 Like

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