问题
I use the ORM sequelize and the postgresql database. And I want to implement pagination.
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)
}
}
But since I use the postgresql database, I have such errors.
SprWellplatforms.findAll(...).skip is not a function
SprWellplatforms.findAll(...).limit is not a function
How can they be fixed?
回答1:
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)
来源:https://stackoverflow.com/questions/53144258/how-to-implement-pagination-correctly-for-nodejs