INNER CODE UNIT · JavaScript
paginate
mrmodise/nodejs-ecommerce-store · routes/main.js:17
function paginate(req, res, next) {
// step through products 9 times
const perPage = 9;
//TODO: refactor below code further
const page = req.params.page;
Product.find()
.skip(perPage * page)
.limit(perPage)
.populate("category")
.exec((err, products) => {
if (err) return next(err);
Product.count().exec((err, count) => {
if (err) return next(err);
res.render("main/product-main", {
products: products,
pages: count / perPage,
});