diff --git a/lib/connection.js b/lib/connection.js index 0c03f6f5116..a73be3270a6 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -1113,7 +1113,7 @@ Connection.prototype.model = function(name, schema, collection, options) { if (!schema.instanceOfSchema) { schema = new Schema(schema); } else if (!(schema instanceof this.base.Schema)) { - schema = this.base.Schema.prototype.clone.call(schema); + schema = schema._clone(this.base.Schema); } } if (schema && !schema.instanceOfSchema) { diff --git a/lib/schema.js b/lib/schema.js index 8c340562708..f5a08244602 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -321,7 +321,22 @@ Schema.prototype.tree; */ Schema.prototype.clone = function() { - const s = new this.constructor({}, this._userProvidedOptions); + const s = this._clone(); + + // Bubble up `init` for backwards compat + s.on('init', v => this.emit('init', v)); + + return s; +}; + +/*! + * ignore + */ + +Schema.prototype._clone = function _clone(Constructor) { + Constructor = Constructor || (this.base == null ? Schema : this.base.Schema); + + const s = new Constructor({}, this._userProvidedOptions); s.base = this.base; s.obj = this.obj; s.options = utils.clone(this.options); @@ -358,9 +373,6 @@ Schema.prototype.clone = function() { s.aliases = Object.assign({}, this.aliases); - // Bubble up `init` for backwards compat - s.on('init', v => this.emit('init', v)); - return s; }; @@ -952,14 +964,12 @@ Schema.prototype.interpretAsType = function(path, obj, options) { // new Schema({ path: [new Schema({ ... })] }) if (cast && cast.instanceOfSchema) { if (!(cast instanceof Schema)) { - try { - cast = Schema.prototype.clone.call(cast); - } catch (err) { - throw new TypeError('Schema for array path `' + path + - '` is from a different, incompatible copy of the Mongoose module. ' + - 'Please make sure you\'re using the same version ' + - 'of Mongoose everywhere with `npm list mongoose`.'); - } + throw new TypeError('Schema for array path `' + path + + '` is from a different copy of the Mongoose module. ' + + 'Please make sure you\'re using the same version ' + + 'of Mongoose everywhere with `npm list mongoose`. If you are still ' + + 'getting this error, please add `new Schema()` around the path: ' + + `${path}: new Schema(...)`); } return new MongooseTypes.DocumentArray(path, cast, obj); } @@ -967,14 +977,12 @@ Schema.prototype.interpretAsType = function(path, obj, options) { cast[options.typeKey] && cast[options.typeKey].instanceOfSchema) { if (!(cast[options.typeKey] instanceof Schema)) { - try { - cast[options.typeKey] = Schema.prototype.clone.call(cast[options.typeKey]); - } catch (err) { - throw new TypeError('Schema for array path `' + path + - '` is from a different, incompatible copy of the Mongoose module. ' + - 'Please make sure you\'re using the same version ' + - 'of Mongoose everywhere with `npm list mongoose`.'); - } + throw new TypeError('Schema for array path `' + path + + '` is from a different copy of the Mongoose module. ' + + 'Please make sure you\'re using the same version ' + + 'of Mongoose everywhere with `npm list mongoose`. If you are still ' + + 'getting this error, please add `new Schema()` around the path: ' + + `${path}: new Schema(...)`); } return new MongooseTypes.DocumentArray(path, cast[options.typeKey], obj, cast); }