Wednesday, 15 March 2017

How to use mongoose populate and moongose pagination together?

Full code here: https://github.com/kenpeter/dl_r18_img_back

There is a nice package to hook pagination with mongoose:

According to this, it seems I can use populate with pagination, but I am not able to get it working.

list: function(page = 1, limit = 100){
    return new Promise(function(resolve, reject){
      let options = {
        page: parseInt(page),
        limit: parseInt(limit),
        sort:{
          createdDate: -1 //Sort by Date Added DESC
        }
      };

      /*
      Image
        .paginate({}, options)
        .then(function(res) {
          resolve && resolve(res);
        });
      */

      // NOT WOKRING!!!!!
      Image
        .populate('category')
        .execPopulate()
        .paginate({}, options)
        .then(function(res) {
          resolve && resolve(res);
        });

      /*
      Image
        .find({})
        ..populate('category')
        .exec()
        .then(function(res) {
          resolve && resolve(res);
        });
      */
    });

As you can see there are 2 commented-out code block Image.xxxxx. They are working individually.

How do I put them together?



via kenpeter

No comments:

Post a Comment