Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

220
Vistas
Paginate running correctly but not passing objects to client

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?

about 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda