mongoosejs / mongoose-lean-getters

Apply getters on lean() documents: https://mongoosejs.com/docs/tutorials/lean.html
Apache License 2.0
11 stars 16 forks source link

TypeError: this.isPathSelectedInclusive is not a function #27

Closed dungkaka closed 1 year ago

dungkaka commented 1 year ago

This bug happens when I populate document have subdocument that also uses getter. And I go to file: node_modules/mongoose-lean-getters/index.js:84:15 to check and no function isPathSelectedInclusive declare !

function applyGettersToDoc(schema, doc, fields, prefix) {
  if (doc == null) {
    return;
  }
  if (Array.isArray(doc)) {
    for (let i = 0; i < doc.length; ++i) {
      applyGettersToDoc.call(this, schema, doc[i], fields, prefix);
    }
    return;
  }
  schema.eachPath((path, schematype) => {
    const pathWithPrefix = prefix ? prefix + '.' + path : path;
    if (this.selectedInclusively() &&
        fields &&
        fields[pathWithPrefix] == null &&
        !this.isPathSelectedInclusive(pathWithPrefix)) { // fields[pathWithPrefix] should return false
      return;
    }
    if (this.selectedExclusively() &&
        fields &&
        fields[pathWithPrefix] != null &&
        !this.isPathSelectedInclusive(pathWithPrefix)) {
      return;
    }
    if (mpath.has(path, doc)) {
      mpath.set(path, schematype.applyGetters(mpath.get(path, doc), doc, true), doc);
    }
  });
}

Versions: "mongoose": "^7.0.3", "mongoose-lean-getters": "^1.0.0",

vkarpov15 commented 1 year ago

mongoose-lean-getters 1.x requires Mongoose 7.1 or greater, because isPathSelectedInclusive was added in Mongoose 7.1. Upgrade to Mongoose ^7.1.0 and this issue will go away.

orimdominic commented 9 months ago

Is there anyway that this can be added to the documentation or at least the README file? Not knowing this caused me days of work. Thank you for your work in this space.

vkarpov15 commented 9 months ago

@orimdominic "peerDependencies" lists Mongoose 7.1.0 as the minimum supported version of Mongoose for this plugin, npm install will log a warning if you try to install this package with a version of Mongoose that's < 7.1.0. We'll add the minimum supported version to the README, but I'd recommend you not ignore peerDependencies warnings.