1

I have implemented the following code for pagination but I am unable to make it work. I need to complete it with the following json data:

following code is for Vt.js file

const express = require('express');
const app = express();
const cors = require('cors');
var MongoClient = require('mongodb').MongoClient;
app.use(cors())
var url = "mongodb://localhost:27017/";

var pagination = require('pagination');
var paginator = pagination.create('search', {
  prelink: '/users',
  current: 2,
  rowsPerPage: 2,
  totalResult: 10020
});
console.log(paginator.render());

app.get('/users', function(req, res) {

  MongoClient.connect(url, function(err, db) {
    if (err)
      throw err;
    var dbo = db.db("MyNewDatabase");
    var data = dbo.collection("VirtualCapitalDB").find({}).toArray(function(err, result) {
      if (err)
        throw err;
      console.log(result);
      res.json(result);

      db.close();
    });
  });
})

app.listen(8080, ()=>console.log('Listening on port 8080'));

once you access it without pagination you can get following json array

[{
  "_id": "5bcb3c77dc56e939187c13a5",
  "firstname": "dumindu",
  "lastname": "nagasinghe",
  "videocount": 5
}, {
  "_id": "5bcb3ce6dc56e939187c13a9",
  "firstname": "cha",
  "lastname": "advv",
  "videocount": 10
}, {
  "_id": "5bcb3d4bdc56e939187c13ab",
  "firstname": "dvvs",
  "lastname": "scvssv",
  "videocount": 4
}, {
  "_id": "5bcb3d7adc56e939187c13ac",
  "firstname": "advav",
  "lastname": "dvdvv",
  "videocount": 5
}, {
  "_id": "5bcb40f7a768f83918480a2b",
  "firstname": "advav",
  "lastname": "dvdvv",
  "videocount": 5
}, {
  "_id": "5bcb4103a768f83918480a2c",
  "firstname": "advav",
  "lastname": "dvdvv",
  "videocount": 5
}, {
  "_id": "5bcb4106a768f83918480a2d",
  "firstname": "advav",
  "lastname": "dvdvv",
  "videocount": 5
}, {
  "_id": "5bcb4125a768f83918480a2e",
  "firstname": "advav",
  "lastname": "dvdvv",
  "videocount": 5
}]

But I need to add pagination and get the 2 data objects for single page. How to implement that in code?

3 Answers 3

4

Best way to write pagination of array items

paginator(items, page, per_page) {

  var page = page || 1,
  per_page = per_page || 10,
  offset = (page - 1) * per_page,

  paginatedItems = items.slice(offset).slice(0, per_page),
  total_pages = Math.ceil(items.length / per_page);
  return {
  page: page,
  per_page: per_page,
  pre_page: page - 1 ? page - 1 : null,
  next_page: (total_pages > page) ? page + 1 : null,
  total: items.length,
  total_pages: total_pages,
  data: paginatedItems
  };
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this module.

// Fetch all running lent loans
module.exports.fetchLends = function (req, res, next) {
    var perPage = 5;
    var page = req.body.page || 1;   // req.body.page pass from client, which page required.
    loans
        .find({
            lenderId: req.user._id,
            _disbursed: true,
            _approved: true,
            _completed: false
        })
        .select(
            "lenderId _disbursed _approved _completed userId disbursed_timestamp status principal lender_interest_amount emi_type completed_timestamp disbursed_timestamp emi.lender_interest_amount emi._disbursed emi._completed emi.principal emi.lender_interest_amount emi.status emi.due_date emi.extension_date_1 emi.extension_date_2 emi.extension_date_3"
        )
        .populate({
            path: "userId",
            select: "name"
        })
        .skip(perPage * page - perPage)
        .limit(perPage)
        .sort({
            disbursed_timestamp: -1
        })
        .exec(function (err, loan) {
console.log(loan)
)}
}

Comments

-1

You want to use the express-paginate module, it's better suited for this case. See the documentation on their github page.

The pagination module you are using needs to create a new http request to get the data, it's not what you need for your application.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.