From d69ab22c140e0e32efceee5b4a8d6efbe12c9d77 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sun, 20 Oct 2019 12:01:44 -0400 Subject: [PATCH] fix(document): support calling `Document` constructor directly in Node.js Fix #8237 --- lib/document.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/document.js b/lib/document.js index c725b2083ab..e108d55679c 100644 --- a/lib/document.js +++ b/lib/document.js @@ -10,6 +10,7 @@ const MongooseError = require('./error/index'); const MixedSchema = require('./schema/mixed'); const ObjectExpectedError = require('./error/objectExpected'); const ObjectParameterError = require('./error/objectParameter'); +const Schema = require('./schema'); const StrictModeError = require('./error/strict'); const ValidatorError = require('./schematype').ValidatorError; const VirtualType = require('./virtualtype'); @@ -64,6 +65,17 @@ function Document(obj, fields, skipId, options) { } options = options || {}; + // Support `browserDocument.js` syntax + if (this.schema == null) { + const _schema = utils.isObject(fields) && !fields.instanceOfSchema ? + new Schema(fields) : + fields; + this.$__setSchema(_schema); + fields = skipId; + skipId = options; + options = arguments[4] || {}; + } + this.$__ = new InternalCache; this.$__.emitter = new EventEmitter(); this.isNew = 'isNew' in options ? options.isNew : true;