Exercise tracker date filtering

I am trying to filter the log output when a from or to parameters are passed to the get route but for some reason I keep getting a type error : TypeError: Cannot read property 'log' of null. I have created the log array field for the user object, which contains individual exercise objects that can be filtered based on query date parameters. Please could you explain why I am getting “log” is null. My code for the get route is:

app.get('/api/users/:_id/logs?', (req, res) => {
  let qryParams = { 
    _id: req.params._id,
  }
  try {
    if (req.query.from && req.query.to) {
    qryParams.date = {
      $gte : new Date(req.query.from).toDateString(),
      $lte : new Date(req.query.to).toDateString()
    }
    } else if (req.query.from) {
      qryParams.date = {$gte : new Date(req.query.from).toDateString()};
    } else if (req.query.to) {
      qryParams.date = {$lte : new Date(req.query.to).toDateString()};
    }
  } catch (err) { 
    return console.log(err);
    }
  

    Users.findOne(qryParams, (err, user) => {
      if (err) return console.log(err);
      var filteredLog;
      if (req.query.limit) {
        filteredLog = user.log.slice(0,req.query.limit);
      } else {
        console.log("user.log: " + user.log);
        filteredLog = user.log;
      }

      if (filteredLog.length == 0) {
        res.send("No exercises found.");
      } else {
        return res.json({
      _id: user._id,
      username: user.username,
      log: filteredLog,
      count: filteredLog.length
    });
      }
      
  });    

});

Without a live project to debug or the actual line number for the error, it’s harder to debug but I would guess the error is coming from here if it’s coming from this code. Also, we don’t have your models or schema, so I don’t know what all is in Users (are you storing a log in there or are you creating upon request?). Regardless, the object user is being set to null. Since Users.findOne() is attempting to set users, it must not be working. It doesn’t seem correct that your Users model would have dates anyway. Maybe you have a separate exercise model?

Of course, all speculation without a project. I would console.log() req.query at the top of the route to check the input and then log users as soon as it’s created and log the log before and after each step of filtering to check everything and run the fCC tests against it.

1 Like

Apologies, I should have provided more information, I am still getting used to posting on the forum :relaxed:

Here is the a link to the live project. Thank you for the suggestions, I will try the steps you have suggested.

This find

    Users.findOne(qryParams, (err, user) => {

will use qryParams.date = {$gte : new Date(req.query.from).toDateString()}; as the filter, but your schema

const userSchema = new Schema({
  _id: { type: mongoose.Types.ObjectId, auto: true },
  username: String,
  log: [{description: String, duration: Number, date: Date}]
});

doesn’t define a date for the user, it’s in the record array. I’ve never used arrays in a mongoose schema like this but here are some sources that might could help. I don’t know if this is a common use case. I have always implemented relationships like this with separate user and exercise records linked by the user id. Mongoose may actually do this internally; maybe someone else could enlighten us.

Since you have the user id in req.params._id, you can find the user with that, get the log, and then filter it based on dates or length.

1 Like

Thank you for your reply. :grin: I have successfully implemented your suggestion of filtering the log after finding the user using _id rather than setting the filtering parameters beforehand.

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