Skip to content

Commit

Permalink
BREAKING CHANGE: make MongooseArray#map() return a plain JavaScript…
Browse files Browse the repository at this point in the history
… array rather than a headless Mongoose array

Re: #8356
  • Loading branch information
vkarpov15 committed May 10, 2020
1 parent 36552fa commit b99f6d3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
4 changes: 3 additions & 1 deletion lib/plugins/idGetter.js
Expand Up @@ -11,10 +11,12 @@ module.exports = function addIdGetter(schema) {
!schema.options.noVirtualId &&
schema.options.id;
if (!autoIdGetter) {
return;
return schema;
}

schema.virtual('id').get(idGetter);

return schema;
};

/*!
Expand Down
7 changes: 2 additions & 5 deletions lib/types/documentarray.js
Expand Up @@ -39,12 +39,9 @@ class CoreDocumentArray extends CoreMongooseArray {
*/

map() {
const ret = super.map.apply(this, arguments);
ret[arraySchemaSymbol] = null;
ret[arrayPathSymbol] = null;
ret[arrayParentSymbol] = null;
const copy = [].concat(this);

return ret;
return Array.prototype.map.apply(copy, arguments);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions test/types.documentarray.test.js
Expand Up @@ -10,6 +10,7 @@ const DocumentArray = require('../lib/types/documentarray');
const ArraySubdocument = require('../lib/types/ArraySubdocument');
const assert = require('assert');
const co = require('co');
const idGetter = require('../lib/plugins/idGetter');
const setValue = require('../lib/utils').setValue;

const mongoose = require('./common').mongoose;
Expand Down Expand Up @@ -39,7 +40,7 @@ function TestDoc(schema) {
title: { type: String }
});

Subdocument.prototype.$__setSchema(schema || SubSchema);
Subdocument.prototype.$__setSchema(idGetter(schema || SubSchema));

return Subdocument;
}
Expand Down Expand Up @@ -575,7 +576,7 @@ describe('types.documentarray', function() {
assert.equal(doc.docs.length, 2);
});

it('map() works', function() {
it('map() works (gh-8317)', function() {
const personSchema = new Schema({ friends: [{ name: { type: String } }] });
mongoose.deleteModel(/Test/);
const Person = mongoose.model('Test', personSchema);
Expand Down

0 comments on commit b99f6d3

Please sign in to comment.