router.get('/news/list', function (req, res, next) {
var limit = req.param("limit", 10);
var currentPage = req.param("currentPage", 1);
if (currentPage < 1) {
currentPage = 1;
}
News.find({"state": 1}).exec(function (err, rs) {
if (err) {
res.send(err);
} else {
var totalPage = Math.floor(rs.length / limit);
if (rs.length % limit != 0) {
totalPage += 1;
}
if (currentPage > totalPage) {
currentPage = totalPage;
}
console.log(currentPage);
var query = News.find({"state": 1});
query.skip((currentPage - 1) * limit);
query.limit(limit);
query.exec(function (err, result) {
jsonArray = {totalCount: rs.length, data: result};
res.json(jsonArray);
});
}
});
});
node.js mongodb分页查询
最新推荐文章于 2023-11-03 20:20:54 发布