Skip to content

Commit

Permalink
fix(query): run default functions after hydrating the loaded document
Browse files Browse the repository at this point in the history
Fix #7182
  • Loading branch information
vkarpov15 committed Nov 1, 2018
1 parent 320d5f8 commit 526f82d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
8 changes: 7 additions & 1 deletion lib/document.js
Expand Up @@ -118,7 +118,13 @@ function Document(obj, fields, skipId, options) {
// Function defaults get applied **after** setting initial values so they
// see the full doc rather than an empty one, unless they opt out.
// Re: gh-3781, gh-6155
$__applyDefaults(this, fields, skipId, exclude, hasIncludedChildren, false, options.skipDefaults);
if (options.willInit) {
this.once('init', () => {
$__applyDefaults(this, fields, skipId, exclude, hasIncludedChildren, false, options.skipDefaults);
});
} else {
$__applyDefaults(this, fields, skipId, exclude, hasIncludedChildren, false, options.skipDefaults);
}

this.$__._id = this._id;

Expand Down
24 changes: 1 addition & 23 deletions lib/queryhelpers.js
Expand Up @@ -127,35 +127,13 @@ exports.createModel = function createModel(model, doc, fields, userProvidedField
}
}

const skipDefaults = gatherPaths(doc, {}, '');
return new model(undefined, fields, {
skipId: true,
isNew: false,
skipDefaults: skipDefaults
willInit: true
});
};

/*!
*
*/

function gatherPaths(obj, map, path) {
for (const key of Object.keys(obj)) {
const fullPath = path ? path + '.' + key : key;
map[fullPath] = true;
if (obj[key] != null &&
typeof obj[key] === 'object' &&
!Array.isArray(obj) &&
!(obj instanceof Map) &&
!obj[key]._bsontype &&
!utils.isMongooseObject(obj[key])) {
gatherPaths(obj[key], map, fullPath);
}
}

return map;
}

/*!
* ignore
*/
Expand Down

0 comments on commit 526f82d

Please sign in to comment.