diff --git a/lib/document.js b/lib/document.js index 87e491bd52c..845b6b3add3 100644 --- a/lib/document.js +++ b/lib/document.js @@ -4418,7 +4418,7 @@ Document.prototype.$populated = Document.prototype.populated; * doc.$assertPopulated('other path'); // throws an error * * - * @param {String|String[]} paths + * @param {String|String[]} path * @return {Document} this * @memberOf Document * @method $assertPopulated @@ -4426,14 +4426,18 @@ Document.prototype.$populated = Document.prototype.populated; * @api public */ -Document.prototype.$assertPopulated = function $assertPopulated(paths) { - if (Array.isArray(paths)) { - paths.forEach(path => this.$assertPopulated(path)); +Document.prototype.$assertPopulated = function $assertPopulated(path, values) { + if (Array.isArray(path)) { + path.forEach(p => this.$assertPopulated(p, values)); return this; } - if (!this.$populated(paths)) { - throw new MongooseError(`Expected path "${paths}" to be populated`); + if (arguments.length > 1) { + this.$set(values); + } + + if (!this.$populated(path)) { + throw new MongooseError(`Expected path "${path}" to be populated`); } return this; diff --git a/types/document.d.ts b/types/document.d.ts index 659db51c10b..897527c45ca 100644 --- a/types/document.d.ts +++ b/types/document.d.ts @@ -26,7 +26,7 @@ declare module 'mongoose' { __v?: any; /** Assert that a given path or paths is populated. Throws an error if not populated. */ - $assertPopulated(paths: string | string[]): Omit & Paths; + $assertPopulated(path: string | string[], values?: Partial): Omit & Paths; /* Get all subdocs (by bfs) */ $getAllSubdocs(): Document[];