File tree Expand file tree Collapse file tree 1 file changed +17
-0
lines changed
Expand file tree Collapse file tree 1 file changed +17
-0
lines changed Original file line number Diff line number Diff 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' ,
You can’t perform that action at this time.
0 commit comments