mongoosejs / mongoose-lean-virtuals

Attach virtuals to the results of mongoose lean() queries
Apache License 2.0
45 stars 24 forks source link

Avoid undefined doc from being virtualized #24

Closed AlexandreMagniez closed 5 years ago

AlexandreMagniez commented 5 years ago

Following this commit nested schemas are virtualized. But I've faced some issue due to this since some of my documents are not strictly following the Mongoose schema I've defined.

If we define the following Mongoose schema :

{
  someKey: String,
  someArray: [
    {
      someOtherKey: String,
    }
  ]
}

... but we have one document like that :

{
  "someKey": "someValue",
}

... a find({}).lean({virtuals: true}) will fail with Cannot read property '_id' of undefined. This PR fixes that undesired behavior.