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

208
Visualizações
How to implement Page-based pagination

Im trying to implement Page-based pagination on this particular endpoint but the problem Im faced is that the response body is not printing out the page, page_size and total results.

The URL is http://localhost:3006/events/tickets?sort_by=name&page=1&page_size=2

How do I get to return a Json body like this ? Ideal Response Im trying to achieve

{
    content: [],               // respponse items
    page: 1,                   // current page
    results_per_page: 10,      // results per page
    total_pages: 10,           // total number of pages
    total_results: 100         // number of all results
}
const getTickets = (tickets) => {
  return new Promise(async (resolve, reject) => {
    try {

      let query = `SELECT id, name, description, ticket_price, ticket_type
      FROM tickets`;

      let sort_by = tickets.sort_by;

      if(sort_by  ?? false) {
                
        query += ` ORDER BY ${sort_by}`;        
        if (sort_by) {
          query += (sort_by == 'desc' ? ` DESC` : ` ASC`);
        } 
      }

      let limit = tickets.page_size;

      let replacements = { limit: limit };

      if (limit) {
        query += ` LIMIT :limit`;
        
        replacements.limit = limit;
      }

      let offset = limit ? ((tickets.page - 1) * limit) : 0;
  
      if(offset) {
        query += ` OFFSET :offset `;
        
        replacements.offset = offset;
      };

      const ticket = await db.sequelize.query(
        query,
        {
          replacements: replacements,
          type: QueryTypes.SELECT,
        }
      );

      return resolve(ticket);
    } catch (err) {
      return reject(err)
    }
  })
}
[
    {
        "id": 1,
        "name": "Tennis Finals",
        "description": "Finals: Tennis Team 1 vs Tennis Team 2",
        "ticket_price": 25,
        "ticket_type": 1
    },
    {
        "id": 5,
        "name": "Testing Tickets",
        "description": "Description....",
        "ticket_price": 1000,
        "ticket_type": 30
    }
]
about 4 years ago · Juan Pablo Isaza
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