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

160
Visualizações
While testing API containing pagination in postman facing skip value error

I am using pagination middleware in my node js project but while testing api in postman it's throwing error skip value

Followed the paginate docs but still no use While testing api in which pagination is applied it's throwing error

below is my pagination middleware

    const pagination = (model) => {
  return async (req, res, next) => {
    const limit = parseInt(req.query.limit);
    const page = parseInt(req.query.page);

    if (page < 1 || page == undefined) {
      page = 1;
    }
    if (limit < 1 || limit == undefined) {
      limit = 2;
    }
    const startIndex = (page - 1) * limit;
    const endIndex = page * limit;
    const result = {};
    model.countDocuments({}, (err, count) => {
      const lengthOfDB = count;
      if (endIndex < lengthOfDB) {
        result.next = {
          page: page + 1,
          //limit: limit,
        };
      }
    }).clone();

    if (startIndex > 0) {
      result.previous = {
        page: page - 1,
        //limit: limit,
      };
    }
    try {
      result.result = await model.find().limit(limit).skip(startIndex).exec();
      res.json(result);
    } catch (e) {
      res.status(500).json({ message: e.message });
    }
  };
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If page is not provided in the query, it will end up being NaN, since this code does not cover that case:

// 'req.query.page' is undefined
let page = parseInt(req.query.page);
if (page < 1 || page == undefined) {
    page = 1;
}
// 'page' is now equal to NaN

A correct way to handle this could be:

let page = parseInt(req.query.page);
if (page < 1 || isNaN(page)) {
    page = 1;
}
about 4 years ago · Juan Pablo Isaza Relatório
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