Mongoose Limit Error - Help

Mongoose Limit Error - Help
0

Anyone know why the limited variable is not working? When I pass a number, it works, but when I pass the req.limited variable, I get an error about the req.dataExercise.length.

// GET user log
app.use("/api/exercise/log?", (req, res, next) => {
    req.userId = req.query.userId;
    req.limited = req.query.limit;
    exerciseModel
      .find({ userid: req.userId }, (err, dataExercise) => {
        req.dataExercise = dataExercise;
        next();
      })
      .limit(3);
  // TODO: limit not working
  },
  (req, res, next) => {
    userModel.findOne({ _id: req.userId }, (err, dataUser) => {
      res.json({
        username: dataUser.username,
        count: req.dataExercise.length,
        log: req.dataExercise
      });
    });
  }
);

Project Code: https://glitch.com/edit/#!/exercise-tracker-stefansen

Thank you!
Cheers :+1:

@bstefansen Make sure you convert limited to a number or you will have trouble.

1 Like

Dammit! It’s always something so simple. Thank you sir!!! You’re a gentleman and a scholar :+1:

May I ask why you are using the limit method?

Never mind. I see now from the sample project.

1 Like

FYI - You will notice the sample project is using a querystring like:

/api/exercise/log?{userid}

and then using querystring parameters like limit=3

1 Like