mongoose-paginate-v2 icon indicating copy to clipboard operation
mongoose-paginate-v2 copied to clipboard

mongoosePaginate.options block toJSON and toObject

Open richardpickett opened this issue 3 years ago • 3 comments

Describe the bug Whenever you set paginate options on the schema, any toJSON and toObject calls you have won't be called.

To Reproduce Steps to reproduce the behavior:

  1. On an existing collection that paginate() currently works on, add toJSON:
// THIS:
  schema.options.toJSON = {
   transform: (doc, ret) => {console.log("your data", ret); return ret;}
  };
// OR THIS:
  schema.method("toJSON", () => {console.log("your data", ret); return ret;});
  1. Run your paginate call like normal, you should see the console log "your data" along with the results.

  2. Then add paginate options prior to adding pagination, it doesn't seem to matter what the options are, you can even set them to a blank object, {}:

mongoosePaginate.paginate.options = {
  lean: true,
  customLabels: {
    totalDocs: "total",
    totalPages: "pageCount",
  },
};

// .....

schema.plugin(mongoosePaginate);
  1. Now when your pagination runs you won't get the console log as before.

Please note, the console log is just for testing, the point is toJSON/toObject never get called if you add any pagination options.

Expected behavior Setting pagination options shouldn't interfere with other calls like toJSON.

Screenshots NA

Desktop (please complete the following information):

  • OS: linux
  • Browser NA
  • Version 1.6.2

Smartphone (please complete the following information): NA

Additional context NA

richardpickett avatar Feb 22 '22 16:02 richardpickett

I have spent sometime with this, and no idea what's causing this issue.

aravindnc avatar Jun 24 '22 04:06 aravindnc

I am facing this issue also. I am using this plugin with @meanie/mongoose-to-json, but all private fields are leaked.

baladin avatar Jul 01 '22 05:07 baladin

any update on this? I'm facing the same issue

mtShaikh avatar Sep 14 '23 01:09 mtShaikh