mongoosejs / mongoose-lean-getters

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

Array values cannot be transformed to non-arrays #37

Open DesignByOnyx opened 4 months ago

DesignByOnyx commented 4 months ago

Do you want to request a feature or report a bug?

Bug - pretty much the same as #36 but with a broader scope.

What is the current behavior?

applyGetters assumes that an array will always be returned and attempts to map the returned value. We have some string arrays which return non-arrays in certain situations, and it worked with 0.4.0.

TypeError: Cannot read properties of undefined (reading 'map')
    at /mongoose-test/node_modules/mongoose-lean-getters/index.js:130:67
   ...

If the current behavior is a bug, please provide the steps to reproduce.

Here's the offending schema definition:

foo: {
  type: [String],
  get(val) => !val || !val.length ? undefined : (val.length === 1 ? val[0] : val),
  set(val) => typeof val === 'string' ? [val] : val
}

What is the expected behavior?

The above should not throw an error.

What are the versions of Node.js, mongoose-lean-getters, and Mongoose are you are using? Note that "latest" is not a version.

Node: 20.12.2 mongoose-lean-getters: 1.1.0 and 2.1.0 mongoose: 7.7.0 and 8.5.0

Note: I have a fix coming, and it will fix #36 too.