aravindnc / mongoose-paginate-v2

A custom pagination library for Mongoose with customizable labels.

Home Page:https://www.npmjs.com/package/mongoose-paginate-v2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mongoosePaginate.options block toJSON and toObject

richardpickett opened this issue · 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

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

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

commented

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