My paginate function is returning the documents correctly on the console but it’s not properly passing them to the client. I have the paginate function set as a middleware below:
module.exports.paginatedResults = async (req, res, next) => {
const page = parseInt(req.query.page);
const limit = parseInt(req.query.limit);
const startIndex = (page - 1) * limit;
const endIndex = page * limit;
const results = {}
if(endIndex < await Event.countDocuments().exec()) {
results.next = {
page: page + 1,
limit: limit
}
}
if(startIndex > 0) {
results.previous = {
page: page - 1,
limit: limit
}
}
results.results = await Event.find().limit(limit).skip(startIndex).exec()
res.paginatedResults = results;
console.log(results);
}
I then pass the results to my route handler using below:
const { paginatedResults } = require('../middleware');
router.route('/')
.get(paginatedResults, catchAsync (events.index))
And then would like to display it on my events index page w/ below function:
module.exports.index = async (req, res) => {
res.render('events/index', { paginatedResults })
};
When I hit the route http://localhost:3000/events?page=3&limit=2
, the proper documents log in my terminal (because of the console.log in my paginateResults middleware function) but the browser spins and time’s out.
I know my data-flow is breaking down somewhere because the results
are not being sent to the client, but where?