我正在使用 express-paginate 和 mongoose-paginate
文件结构是 express-generator
我有所有插件的最新版本
示例来自: https://github.com/expressjs/express-paginate#example
每次都会在
发生此错误pages: paginate.getArrayPages(req)(3, pageCount, req.query.page)
我的代码就像
pages: res.locals.paginate.getArrayPages(req)(3, categories.pages, 1)
当我评论这一行时 页面呈现成功,但我之后无法创建链接
路由/ 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);
模型/ 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;
答案 0 :(得分:1)
虽然paginate.getArrayPages()
需要req
作为参数并返回一个函数,但当您使用res.locals.paginate.getArrayPages()
时,req
已经通过(here)并且您不应该不能自己通过。
试试这个:
pages : res.locals.paginate.getArrayPages(3, categories.pages, 1)
同样适用于hasNextPages()
:
has_more : res.locals.paginate.hasNextPages(categories.pages)