Skip to content

Instantly share code, notes, and snippets.

@amandeepmittal
Last active July 20, 2018 15:12
Show Gist options
  • Save amandeepmittal/e49845cb40a6e1b1ff9518209f25bf95 to your computer and use it in GitHub Desktop.
Save amandeepmittal/e49845cb40a6e1b1ff9518209f25bf95 to your computer and use it in GitHub Desktop.
router.get('/:page', (req, res) => {
let limit = 50; // number of records per page
let offset = 0;
db.user.findAndCountAll()
.then((data) => {
let page = req.params.page; // page number
let pages = Math.ceil(data.count / limit);
offset = limit * (page - 1);
db.user.findAll({
attributes: ['id', 'first_name', 'last_name', 'date_of_birth'],
limit: limit,
offset: offset,
$sort: { id: 1 }
})
.then((users) => {
res.status(200).json({'result': users, 'count': data.count, 'pages': pages});
});
})
.catch(function (error) {
res.status(500).send('Internal Server Error');
});
});
@Gasperowicz
Copy link

@amandeepmittal
Copy link
Author

amandeepmittal commented Jun 22, 2017

Yes, indeed. My mistake.
Correction done, thank you!

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