Skip to content

Commit f92f296

Browse files
committed
changes
1 parent b5b8241 commit f92f296

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

controllers/tourController.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ exports.getAllTours = async (req, res) => {
2222

2323
//making a query field to find...
2424
const query = Tour.find(JSON.parse(queryStr));
25+
26+
//adding pagination...
27+
const page = req.query.page * 1 || 1;
28+
const limit = req.query.limit * 1 || 100;
29+
30+
//so here basically what we are doing is seeing that skip
31+
//contain the page - 1 * limit value in which limit is by default set to 10..
32+
const skip = (page - 1) * limit;
33+
34+
query = query.skip(skip).limit(limit);
35+
36+
if (req.query.page) {
37+
//used to counting the number of documents in the page....
38+
const numTours = await Tour.countDocuments();
39+
//if the skip is more than we must throw an error...
40+
if (skip >= numTours) throw new Error('This page is not avaliable!!');
41+
}
2542
const tours = await query;
2643
res.status(200).json({
2744
status: 'success',

0 commit comments

Comments
 (0)