@@ -28,6 +28,7 @@ function paginate(query, options, callback) {
28
28
let leanWithId = options . hasOwnProperty ( 'leanWithId' ) ? options . leanWithId : true ;
29
29
let limit = options . hasOwnProperty ( 'limit' ) ? options . limit : 10 ;
30
30
let page , offset , skip , docsQuery , promises ;
31
+
31
32
if ( options . offset ) {
32
33
offset = options . offset ;
33
34
skip = offset ;
@@ -39,28 +40,25 @@ function paginate(query, options, callback) {
39
40
offset = 0 ;
40
41
skip = offset ;
41
42
}
43
+
42
44
if ( limit > 0 ) {
43
45
docsQuery = this . find ( query )
44
46
. select ( select )
45
47
. sort ( sort )
46
48
. skip ( skip )
47
49
. limit ( limit )
48
50
. lean ( lean ) ;
51
+
49
52
if ( populate ) {
50
- [ ] . concat ( populate ) . forEach ( ( item ) => {
51
- docsQuery . populate ( item ) ;
52
- } ) ;
53
+ [ ] . concat ( populate ) . forEach ( ( item ) => docsQuery . populate ( item ) ) ;
53
54
}
54
55
}
55
- promises = {
56
- docs : ( docsQuery ) ? docsQuery . exec ( ) : false ,
57
- count : this . count ( query ) . exec ( )
58
- } ;
59
- promises = Object . keys ( promises ) . map ( ( index ) => {
60
- if ( promises [ index ] ) {
61
- return promises [ index ] ;
62
- }
63
- } ) ;
56
+
57
+ promises = [
58
+ docsQuery ? docsQuery . exec ( ) : Promise . resolve ( { } ) ,
59
+ this . count ( query ) . exec ( )
60
+ ] ;
61
+
64
62
let promise = new Promise ( ( resolve , reject ) => {
65
63
Promise . all ( promises ) . then ( ( data ) => {
66
64
let docs = ( limit > 0 ) ? data [ 0 ] : [ ] ;
@@ -74,7 +72,7 @@ function paginate(query, options, callback) {
74
72
result . docs = result . docs . map ( ( doc ) => {
75
73
doc . id = String ( doc . _id ) ;
76
74
return doc ;
77
- } ) ;
75
+ } ) ;
78
76
}
79
77
if ( offset !== undefined ) {
80
78
result . offset = offset ;
@@ -94,6 +92,7 @@ function paginate(query, options, callback) {
94
92
reject ( error ) ;
95
93
} ) ;
96
94
} ) ;
95
+
97
96
return promise ;
98
97
}
99
98
0 commit comments