-
-
Save PriteshJain/7d092feeaf71e664d7030eb5faddbc4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
router.get('/:page', (req, res) => { | |
let page = req.params.page; // page number | |
let limit = 50; // number of records per page | |
let offset = page * limit; | |
db.user.findAndCountAll({ | |
attributes: ['id', 'first_name', 'last_name', 'date_of_birth'], | |
limit: limit, | |
offset: offset, | |
$sort: { id: 1 } | |
}).then((data) => { | |
let pages = Math.ceil(data.count / limit); | |
offset = limit * (page - 1); | |
let users = data.rows; | |
res.status(200).json({'result': users, 'count': data.count, 'pages': pages}); | |
}) | |
.catch(function (error) { | |
res.status(500).send('Internal Server Error'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why do you set offset in line 13 again? Shouldn't it be "limit * (page - 1)" in line 4?