-1

The below is my code.I want to increase the page no. whenever there are more than 9 entries i.e a single page should contain only 9 entries.

var pageNo = 1;
        if(tempDetail.length/9 >= 0)
        pageNo++;
        res.status(200).json(
            {   
                totalTemplatesinDb:tempDetail.length,
                pages:pageNo,
                totalTemplates: ((BTtemplata.length) + (Shtemplata.length) + (FNtemplata.length)),
                groupName: [{ "name": "Telecom", totalTemplate: BTtemplata.length, template: BTtemplata }, { "name": "Retail", totalTemplate: Shtemplata.length, template: Shtemplata },{"name": "Finance", totalTemplate: FNtemplata.length, template: FNtemplata}],
            });
    }
    catch (error) {
      
        console.log(error);
        res.status(500).json({
            error: err
        });
    }
};
  • 1
    Does this answer your question? [Mongoose Pagination and Filters](https://stackoverflow.com/questions/12259730/mongoose-pagination-and-filters) – Chance Mar 16 '21 at 16:12

1 Answers1

0

Why don't you use it like this:

try {

    var pageNo = Math.floor(tempDetail.length / 9);

    res.status(200).json({
        totalTemplatesinDb: tempDetail.length,
        pages: pageNo,
        totalTemplates: ((BTtemplata.length) + (Shtemplata.length) + (FNtemplata.length)),
        groupName: [{
            "name": "Telecom",
            totalTemplate: BTtemplata.length,
            template: BTtemplata
        }, {
            "name": "Retail",
            totalTemplate: Shtemplata.length,
            template: Shtemplata
        }, {
            "name": "Finance",
            totalTemplate: FNtemplata.length,
            template: FNtemplata
        }],
    });
} catch (error) {

    console.log(error);
    res.status(500).json({
        error: error.message
    });
};
Murat Colyaran
  • 467
  • 1
  • 12