我已经实现了以下用于分页的代码,但无法使其正常工作。我需要使用以下json数据完成此操作:
以下代码适用于Vt.js
文件
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'));
一旦您无需分页就可以访问它,您可以获取以下json数组
[{
"_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
}]
但是我需要添加分页并为单个页面获取2个数据对象。如何在代码中实现?
答案 0 :(得分:0)
您想使用express-paginate模块,它更适合这种情况。请参阅其github页面上的文档。
您使用的pagination模块需要创建一个新的http请求以获取数据,这不是您的应用程序所需要的。
答案 1 :(得分:0)
编写数组项分页的最佳方法
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
};
}
答案 2 :(得分:0)
尝试此模块。
// 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)
)}
}