From 7bd3663f61460e0c2e2b6854ddcf9d8be72f6028 Mon Sep 17 00:00:00 2001 From: Hafez Date: Sun, 17 Nov 2019 22:26:09 +0200 Subject: [PATCH] Fixes #8317 map(...) --- lib/types/documentarray.js | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/types/documentarray.js b/lib/types/documentarray.js index 87757e4f171..9ac61bf69fd 100644 --- a/lib/types/documentarray.js +++ b/lib/types/documentarray.js @@ -180,6 +180,15 @@ class CoreDocumentArray extends CoreMongooseArray { return arr; } + map() { + const arr = super.map.apply(this,arguments); + + arr[arrayParentSymbol] = this[arrayParentSymbol]; + arr[arrayPathSymbol] = this[arrayPathSymbol]; + + return arr; + } + /** * Wraps [`Array#push`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/push) with proper change tracking. * @@ -328,18 +337,18 @@ if (util.inspect.custom) { function _updateParentPopulated(arr) { const parent = arr[arrayParentSymbol]; - if (parent.$__.populated != null) { - const populatedPaths = Object.keys(parent.$__.populated). - filter(p => p.startsWith(arr[arrayPathSymbol] + '.')); + if (parent.$__.populated == null) return; - for (const path of populatedPaths) { - const remnant = path.slice((arr[arrayPathSymbol] + '.').length); - if (!Array.isArray(parent.$__.populated[path].value)) { - continue; - } + const populatedPaths = Object.keys(parent.$__.populated). + filter(p => p.startsWith(arr[arrayPathSymbol] + '.')); - parent.$__.populated[path].value = arr.map(val => val.populated(remnant)); + for (const path of populatedPaths) { + const remnant = path.slice((arr[arrayPathSymbol] + '.').length); + if (!Array.isArray(parent.$__.populated[path].value)) { + continue; } + + parent.$__.populated[path].value = arr.map(val => val.populated(remnant)); } }