Skip to content

Commit 3468f93

Browse files
authored
Fix: undefined document with mongoose options (new && rawResult) (#70)
1 parent cdb1dd6 commit 3468f93

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

lib/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ exports.default = function (schema, opts) {
197197
return postUpdateOne.call(this, {}, next);
198198
}
199199

200+
if (this.options.new && this.options.rawResult) {
201+
doc = doc.value;
202+
}
203+
200204
doc._original = this._original;
201205
createPatch(doc, this.options).then(function () {
202206
return next();

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ export default function (schema, opts) {
335335
return postUpdateOne.call(this, {}, next)
336336
}
337337

338+
if (this.options.new && this.options.rawResult) {
339+
doc = doc.value
340+
}
341+
338342
doc._original = this._original
339343
createPatch(doc, this.options)
340344
.then(() => next())

test/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,16 @@ describe('mongoose-patch-history', () => {
371371
})
372372
.catch(done)
373373
})
374+
375+
it('should work with options { new:true, rawResult:true }', (done) => {
376+
Post.findOneAndUpdate(
377+
{ title: 'findOneAndUpdate2' },
378+
{ title: 'findOneAndUpdate1' },
379+
{ new: true, rawResult: true }
380+
)
381+
.then(() => done())
382+
.catch(done)
383+
})
374384
})
375385

376386
describe('updating a document via updateOne()', () => {

0 commit comments

Comments
 (0)