Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda