Skip to content

Limit option on Find() not working #93

Closed

Description

@gurufaction

I am adding the limit and offset options to my find method but only the offset is being applied. I also tried hard coding the values but I got the same result.

module.exports = function (app) {
    app.get("/customer", function (req, res) {
        req.db.models.Customer.find({ }, { limit: req.query.limit, offset: req.query.offset}, function (err, results) {
            var response = {
                results : [],
                success : false
            };
            response.total = results.length;
            response.results = results;
            res.send(response);
        });
    });

Activity

  1. dresende commented on Mar 26, 2013

    @dresende
    Owner

    Sorry for the late response. There's no limit option in there, but I'm going to add it. You have 2 ways to change your code to have limit:

    1

    req.db.models.Customer.find().limit(req.query.limit).offset(req.query.offset).get(function (err, results) {
        // ...
    });

    2

    req.db.models.Customer.find({}, req.query.limit, { offset: req.query.offset }, function (err, results) {
        // ...
    });

    You can mix them, but limit is not available yet in the .find() options parameter.

  2. gurufaction commented on Mar 27, 2013

    @gurufaction
    Author

    I tried your second suggestion and received an error in "order clause". I will pull your latest code and try that.

  3. dresende commented on Mar 27, 2013

    @dresende
    Owner

    That's probably because req.query.limit is a string, it must be a number (use parseInt).

  4. gurufaction commented on Mar 28, 2013

    @gurufaction
    Author

    This is the only version that worked for me. The second example seems to ignore the offset even with the parseInt.

    module.exports = function (app) {
        app.get("/comment", function (req, res) {
            req.db.models.Comment.find().limit(parseInt(req.query.limit)).offset(parseInt(req.query.offset)).run(function (err, results) {
                console.log(err);
                var response = {
                    results : [],
                    success : false
                };
                response.total = results.length;
                response.results = results;
                res.send(response);
            });
        });
  5. dresende commented on Mar 28, 2013

    @dresende
    Owner

    I found the issue, it was because when doing { offset: ... } it was replacing the options and so the previously set limit was discarded. If you switch the order of the arguments it wouldn't happen. Anyway, I did a small fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions