Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit option on Find() not working #93

Closed
gurufaction opened this issue Mar 24, 2013 · 5 comments
Closed

Limit option on Find() not working #93

gurufaction opened this issue Mar 24, 2013 · 5 comments

Comments

@gurufaction
Copy link

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);
        });
    });
@dresende
Copy link
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.

dresende added a commit that referenced this issue Mar 26, 2013
It's still available has it was, the move is internal
@gurufaction
Copy link
Author

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

@dresende
Copy link
Owner

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

@gurufaction
Copy link
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);
        });
    });

@dresende
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants