Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,9 @@ Document.prototype.$init = function() {
*/

Document.prototype.$__init = function(doc, opts) {
if (doc == null) {
throw new ObjectParameterError(doc, 'doc', 'init');
}
this.$isNew = false;
opts = opts || {};

Expand Down
26 changes: 26 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const ObjectId = Schema.Types.ObjectId;
const DocumentObjectId = mongoose.Types.ObjectId;
const EmbeddedDocument = mongoose.Types.Subdocument;
const MongooseError = mongoose.Error;
const ObjectParameterError = require('../lib/error/objectParameter');

describe('Model', function() {
let db;
Expand Down Expand Up @@ -9164,6 +9165,31 @@ describe('Model', function() {
assert.strictEqual(doc.name, 'Test2');
});
});
describe('gh-15812', function() {
it('should throw ObjectParameterError when init is called with null', function() {
const doc = new mongoose.Document({}, new mongoose.Schema({ name: String }));
try {
doc.init(null);
assert.fail('Should have thrown an error');
} catch (error) {
assert.ok(error instanceof ObjectParameterError);
assert.strictEqual(error.name, 'ObjectParameterError');
assert.ok(error.message.includes('Parameter "doc" to init() must be an object'));
}
});

it('should throw ObjectParameterError when init is called with undefined', function() {
const doc = new mongoose.Document({}, new mongoose.Schema({ name: String }));
try {
doc.init(undefined);
assert.fail('Should have thrown an error');
} catch (error) {
assert.ok(error instanceof ObjectParameterError);
assert.strictEqual(error.name, 'ObjectParameterError');
assert.ok(error.message.includes('Parameter "doc" to init() must be an object'));
}
});
});
});


Expand Down
Loading