如何为nodejs正确实现分页? [英] How to implement pagination correctly for nodejs?
本文介绍了如何为nodejs正确实现分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 ORM sequelize 和 postgresql 数据库.我想实现分页.
I use the ORM sequelize and the postgresql database. And I want to implement pagination.
使用MongoDB数据库,分页是这样进行的.
Using the MongoDB database, pagination is performed this way.
module.exports.getBySprOilfieldId = async function (req, res) {
try {
const query = {
where: {
spr_oilfields_id: req.params.spr_oilfields_id
}
}
const sprwellplatforms = await SprWellplatforms.findAll(query)
.skip(+req.query.offset)
.limit(+req.query.limit)
res.status(200).json(sprwellplatforms)
} catch(e) {
errorHandler(res, e)
}
}
但是由于我使用的是 postgresql 数据库,所以出现了这样的错误.
But since I use the postgresql database, I have such errors.
SprWellplatforms.findAll(...).skip 不是函数
SprWellplatforms.findAll(...).skip is not a function
SprWellplatforms.findAll(...).limit 不是函数
SprWellplatforms.findAll(...).limit is not a function
如何修复它们?
推荐答案
offset
和 limit
值应该设置在传递给 findAll() 的选项上
如分页/限制.
The offset
and limit
values should be set on the options passed to findAll()
as seen in the documentation for Pagination/Limiting.
const query = {
where: {
spr_oilfields_id: req.params.spr_oilfields_id
},
offset: +req.query.offset,
limit: +req.query.limit,
};
const sprwellplatforms = await SprWellplatforms.findAll(query)
这篇关于如何为nodejs正确实现分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文