How to implement pagination correctly for nodejs?

风格不统一 提交于 2020-06-29 03:58:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!