Skip to content

Commit

Permalink
Merge pull request #8365 from Fonger/fix/insertMany-cast
Browse files Browse the repository at this point in the history
Correctly catch the error when insertMany fails to initialize the document
  • Loading branch information
vkarpov15 committed Nov 23, 2019
2 parents dc9272c + b5b21fb commit 38eb5aa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/model.js
Expand Up @@ -3206,10 +3206,15 @@ Model.$__insertMany = function(arr, options, callback) {

const toExecute = [];
const validationErrors = [];

arr.forEach(function(doc) {
toExecute.push(function(callback) {
if (!(doc instanceof _this)) {
doc = new _this(doc);
try {
doc = new _this(doc);
} catch (err) {
return callback(err);
}
}
if (options.session != null) {
doc.$session(options.session);
Expand Down
12 changes: 12 additions & 0 deletions test/model.test.js
Expand Up @@ -4909,6 +4909,18 @@ describe('Model', function() {
});
});

it('insertMany() with non object array error can be catched (gh-8363)', function(done) {
const schema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
url: { type: String }
});
const Image = db.model('gh8363', schema);
Image.insertMany(['a', 'b', 'c']).catch((error) => {
assert.equal(error.name, 'ObjectParameterError');
done();
});
});

it('insertMany() return docs with empty modifiedPaths (gh-7852)', function() {
const schema = new Schema({
name: { type: String }
Expand Down

0 comments on commit 38eb5aa

Please sign in to comment.