Skip to content

Commit

Permalink
Merge pull request #8351 from AbdelrahmanHafez/gh-8317
Browse files Browse the repository at this point in the history
Fixes #8317 map(...)
  • Loading branch information
vkarpov15 authored Nov 19, 2019
2 parents 9509b47 + 63c2f1e commit b946225
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lib/types/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,18 +328,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 || 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));
}
}

Expand Down
13 changes: 13 additions & 0 deletions test/types.documentarray.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,19 @@ describe('types.documentarray', function() {
assert.equal(arr.length, 1);
assert.equal(doc.docs.length, 2);
});

it('map() works', function() {
const personSchema = new Schema({ friends: [{ name: { type: String } }]});
const Person = mongoose.model('gh8317-map', personSchema);

const person = new Person({ friends: [{ name: 'Hafez' }] });

const friendsNames = person.friends.map(friend => friend.name);
friendsNames.push('Sam');

assert.equal(friendsNames.length, 2);
assert.equal(friendsNames[1], 'Sam');
});
});

it('cleans modified subpaths on splice() (gh-7249)', function() {
Expand Down

0 comments on commit b946225

Please sign in to comment.