danish
danish

Reputation: 125

Error: express-paginate: `limit` is not a number >= 0

i'm using express-paginate and mongoose-paginate

file structure is express-generator

i have latest version of all plugins

example taken from: https://github.com/expressjs/express-paginate#example

this error occurs everytime at

pages: paginate.getArrayPages(req)(3, pageCount, req.query.page)

my code is like

pages: res.locals.paginate.getArrayPages(req)(3, categories.pages, 1)

When I comment this line page renders succesfully but I cannot create links afterwards

routes/category.js

function getAllRecords(req , res , next){
    console.log("getAllRecords");
    var Category = res.categoryModel ; // models/categoryModel.js
     Category.paginate({}, { page: req.query.page, limit: req.query.limit }, function(err, categories) {
 if (err) throw err;
res.format({
            html: function() {
                res.render('category/admin', {
                models: categories.docs,
                'res':res,
                pageCount: categories.pages,
                itemCount: categories.total,
                pages: res.locals.paginate.getArrayPages(req)(3, categories.pages, 1)
                });
            },
            json: function() {
                // inspired by Stripe's API response for list objects
                res.json({
                object: 'list',
                has_more: res.locals.paginate.hasNextPages(req)(categories.pages),
                data: categories.docs
                });
            }
        });

    });


}

app.js

ar app = express();
// keep this before all routes that will use pagination
app.use(paginate.middleware(10, 50));
/***** Models ********/
var categoryModel = require('./models/categoryModel');
var modelsInsideCategoryController = {'categoryModel':categoryModel};
var category = require('./routes/category')(modelsInsideCategoryController);
app.use('/category', category);

models/categoryModel.js

// grab the things we need
var mongoose = require('mongoose');
var mongoosePaginate = require('mongoose-paginate');
var Schema = mongoose.Schema;
// create a schema
var categorySchema = new Schema({
  name: {type:String,required:true},
  created_at: Date,
  updated_at: Date
});
categorySchema.plugin(mongoosePaginate);
var Category = mongoose.model('Category', categorySchema);
make this available to our users in our Node applications
module.exports = Category;

Upvotes: 0

Views: 1386

Answers (1)

robertklep
robertklep

Reputation: 203231

While paginate.getArrayPages() requires req as argument and returns a function, when you use res.locals.paginate.getArrayPages(), req has already been passed (here) and you shouldn't pass it yourself.

Try this:

pages : res.locals.paginate.getArrayPages(3, categories.pages, 1)

And the same applies for hasNextPages() as well:

 has_more : res.locals.paginate.hasNextPages(categories.pages)

Upvotes: 1

Related Questions