Skip to content

Instantly share code, notes, and snippets.

@PriteshJain
Forked from amandeepmittal/user.js
Last active July 20, 2018 15:12
Show Gist options
  • Save PriteshJain/7d092feeaf71e664d7030eb5faddbc4c to your computer and use it in GitHub Desktop.
Save PriteshJain/7d092feeaf71e664d7030eb5faddbc4c to your computer and use it in GitHub Desktop.
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');
});
});
@Raidus
Copy link

Raidus commented Nov 23, 2017

Why do you set offset in line 13 again? Shouldn't it be "limit * (page - 1)" in line 4?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment