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

Fix errors on null value array types #36

Closed Sebmaster closed 1 month ago

Sebmaster commented 3 months ago

Summary

mongoose queries error when processing a record which contains null as a value of an Array schema type.

TypeError: Cannot read properties of null (reading 'map')
    at /mongoose-test/node_modules/mongoose-lean-getters/index.js:130:67
    at Schema.eachPath (/mongoose-test/node_modules/mongoose/lib/schema.js:1569:5)
    at model.Query.applyGettersToDoc (/mongoose-test/node_modules/mongoose-lean-getters/index.js:110:16)
    at model.Query.applyGetters (/mongoose-test/node_modules/mongoose-lean-getters/index.js:51:25)
    at model.Query.<anonymous> (/mongoose-test/node_modules/mongoose-lean-getters/index.js:36:18)
    at next (/mongoose-test/node_modules/kareem/index.js:268:35)
    at Kareem.execPost (/mongoose-test/node_modules/kareem/index.js:296:3)
    at /mongoose-test/node_modules/mongoose/lib/query.js:4380:28
    at new Promise (<anonymous>)
    at _executePostHooks (/mongoose-test/node_modules/mongoose/lib/query.js:4377:10)

With this change we check for non-null after running possible array getters, but before running the for-each-element .map(). If the value is indeed null, we'll bail out of processing array elements, instead just continuing with the non-array codepath.

Fixes #35.

nadavhalfon commented 3 months ago

I think it caused because this fix: https://github.com/mongoosejs/mongoose-lean-getters/pull/32