Exercise Tracker - Can’t pass the last test

Hi, I can’t pass the last test. Here is my code:

app.post('/api/users', async function(req, res) {
  try {
    const username = req.body.username;
    let created = await UserModel.create({ username });
    res.json({
      _id: created._id,
      username: created.username
    });
  } catch (error) {
    console.error(error)
    res.status(500).json('Server erorr...')
  }
});

app.get('/api/users', async function(req, res) {
  try {
    let listUser = await UserModel.find();
    res.json(listUser);
  } catch (error) {
    console.error(error)
    res.status(500).json('Server erorr...')
  }
});

app.post('/api/users/:_id/exercises', async function(req, res) {
  try {
    const { description, duration, date } = req.body;
    const userId = req.params._id;
    let user = await UserModel.findOne({ _id: userId });
    let createExercise = await ExerciseModel.create({
      userId,
      description,
      duration,
      date: date ? date : new Date()
    })
    res.json({
      _id: userId,
      username: user.username,
      ...(createExercise.date && { date: createExercise.date.toDateString() }),
      duration: createExercise.duration,
      description: createExercise.description
    });
  } catch (error) {
    console.error(error)
    res.status(500).json('Server erorr...')
  }
});

app.get('/api/users/:_id/logs', async function(req, res) {
  try {
    const { from, to, limit } = req.query;
    const userId = req.params._id;
    let user = await UserModel.findOne({ _id: userId });
    let filter = {
      userId
    };
    if (from || to) {
      filter.date = {
        ...(from && { $gte: from }),
        ...(to && { $lte: to })
      }
    }
    
    let exercises;
    if (limit) {
      exercises = await ExerciseModel.find(filter).limit(limit).sort({date: -1});
    } else {
      exercises = await ExerciseModel.find(filter).sort({date: -1});
    }
    let log = exercises.map((ex) => ({
      description: ex.description,
      duration: ex.duration,
      ...(ex.date && { date: ex.date.toDateString() })
    }))
    
    res.json({
      _id: userId,
      username: user.username,
      ...(from && { from: (new Date(from)).toDateString() }),
      ...(to && { to: (new Date(to)).toDateString() }),
      count: log.length,
      log
    });
  } catch (error) {
    console.error(error)
    res.status(500).json('Server erorr...')
  }
});

Some things to note about your code:

(1)

let user = await UserModel.findOne({ _id: userId });

If he request parameter for the userId is not a valid one, what is it you need to do? Maybe return a response object like { message: "Invalid user id" }. This applies to the following two cases also, where you can return a “invalid data” message.

(2)

if (from || to) {
    filter.date = {
        ...(from && { $gte: from }),
        ...(to && { $lte: to })
    }
}

What happens if the from and /or to request query string parameters are not valid date strings?

(3)

if (limit) {
    exercises = await ExerciseModel.find(filter).limit(limit)...

What happens if the limit is not a valid value/number? You are to pass the limit value as a number in the code. The Mongoose documentation says that the parameter for the limit needs to be a number (see Query limit).

(4)

The output expected needs to be of the following format. Is your code returning such a response object?

{
  username: "fcc_test",
  count: 1,
  _id: "5fb5853f734231456ccb3b05",
  log: [{
    description: "test",
    duration: 60,
    date: "Mon Jan 01 1990",
  }]
}
1 Like

I tried on different system Glitch and the test is passed!
Thank you so much for the help @Prasadsaya !

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