F
F
friktor2014-03-29 17:43:15
Node.js
friktor, 2014-03-29 17:43:15

How to make pagination in SailsJS?

I decided to start learning SailsJS at first, like everyone else, I watched videocasts with lessons, more or less getting used to it, I began to write code myself, there seemed to be no difficulties, but when I decided to write a simple blog, I ran into a problem with pagination, there is a paginate method in the waterline docks ( ) that performs pagination, or just makes a post limit, the docks are raw, there are only a couple of sentences, and a few lines of code about the method itself, without real use. It seems, of course, after searching for users, break it down into 10 elements. I decided to do something similar and in the simplest version it turned out this
User.find().paginate({page: 2, limit: 10});

index: function (req, res, next) {
  	Blog.find(function (err, blog) {
  		if (err) return next(err);
  		res.view({
  			blog: blog
  		});
  	}).paginate();
  }

It seems to be as described, called as a method, after find (), but when launched, it gives an error - the method was not found. What is the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene Obrezkov, 2014-03-29
@friktor

You fundamentally wrote the sample incorrectly. Need like this:

getImages: function(req, res) {
        var page = req.param('page');
        LibraryImage
            .find()
            .paginate({
                page: page,
                limit: 20
            })
            .done(function(error, images) {
                if (error) {
                    res.serverError(error);
                } else {
                    res.json(images);
                }
            });
    }

paginate() must fire BEFORE the result is displayed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question