Skip to content

Commit

Permalink
Make DocumentArray#map return a regular JS array
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelrahmanHafez committed Nov 18, 2019
1 parent e01c247 commit ba2c353
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
8 changes: 2 additions & 6 deletions lib/types/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,7 @@ class CoreDocumentArray extends CoreMongooseArray {
}

map() {
const arr = super.map.apply(this,arguments);

arr[arrayParentSymbol] = this[arrayParentSymbol];
arr[arrayPathSymbol] = this[arrayPathSymbol];

const arr = [].concat(Array.prototype.map.apply(this,arguments));
return arr;
}

Expand Down Expand Up @@ -337,7 +333,7 @@ if (util.inspect.custom) {

function _updateParentPopulated(arr) {
const parent = arr[arrayParentSymbol];
if (parent.$__.populated == null) return;
if (!parent || parent.$__.populated == null) return;

const populatedPaths = Object.keys(parent.$__.populated).
filter(p => p.startsWith(arr[arrayPathSymbol] + '.'));
Expand Down
3 changes: 2 additions & 1 deletion test/types.documentarray.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,13 +576,14 @@ describe('types.documentarray', function() {
assert.equal(doc.docs.length, 2);
});

it('map() copies parent and path ()', function() {
it('map() works and does not return a mongoose array', 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);
assert.deepEqual(friendsNames.constructor, Array);

friendsNames.push('Sam');

Expand Down

0 comments on commit ba2c353

Please sign in to comment.