codepunkt / mongoose-patch-history

Mongoose plugin that saves a history of JSON patch operations for all documents belonging to a schema in an associated 'patches' collection
MIT License
96 stars 21 forks source link

Fixed undefined document with mongoose options (new && rawResult) #70

Closed vinerich closed 4 years ago

vinerich commented 4 years ago

Hey @codepunkt ! Hope you are well 😄 (#69)

Mongoose findOneAndUpdate accepts several options including new and rawResult. Used alone, these options return the document as expect. Used together however, not the document is returned and used in the plugin callbacks but an object of this format

{
  lastErrorObject: { n: 1, updatedExisting: true },
  value: {
    _id: 5f6cfd8a45d0cb26f8f71a86,
    title: 'foo',
    createdAt: 2020-09-24T20:11:54.779Z,
    updatedAt: 2020-09-24T20:11:54.877Z,
    __v: 0
  },
  ok: 1,
}

As one can see the wanted document is contained inside the value property.

I don't know if this can happen in more than the findOneAndUpdate query. And tbh I'm not in the mood to search the code for possible places it can happen. IMHO this fix can be included as is since the probability that anybody will ran into a similiar issue is low enough.

vinerich commented 4 years ago

Needed forcepush since I accidentaly broke 2 other tests. Should be good to go now.