Skip to content

Commit 98facde

Browse files
authored
Merge pull request #8332 from AbdelrahmanHafez/master
Fixes #8331
2 parents c900645 + a91dc93 commit 98facde

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/helpers/model/castBulkWrite.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ module.exports = function castBulkWrite(model, op, options) {
3434
op = op['updateOne'];
3535
return (callback) => {
3636
try {
37+
if (!op['filter']) throw new Error('Must provide a filter object.');
38+
if (!op['update']) throw new Error('Must provide an update object.');
39+
3740
op['filter'] = cast(model.schema, op['filter']);
3841
op['update'] = castUpdate(model.schema, op['update'], {
3942
strict: model.schema.options.strict,
@@ -61,6 +64,9 @@ module.exports = function castBulkWrite(model, op, options) {
6164
op = op['updateMany'];
6265
return (callback) => {
6366
try {
67+
if (!op['filter']) throw new Error('Must provide a filter object.');
68+
if (!op['update']) throw new Error('Must provide an update object.');
69+
6470
op['filter'] = cast(model.schema, op['filter']);
6571
op['update'] = castUpdate(model.schema, op['update'], {
6672
strict: model.schema.options.strict,

test/model.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5674,6 +5674,34 @@ describe('Model', function() {
56745674
then(() => Model.findOne()).
56755675
then(doc => assert.equal(doc.nested.name, 'foo'));
56765676
});
5677+
5678+
it('throws an error if no update object is provided (gh-8331)', function() {
5679+
const userSchema = new Schema({ name: { type: String, required: true } });
5680+
const User = db.model('gh8331', userSchema);
5681+
5682+
return co(function*() {
5683+
const createdUser = yield User.create({ name: 'Hafez' });
5684+
let threw = false;
5685+
try {
5686+
yield User.bulkWrite([{
5687+
updateOne: {
5688+
filter: { _id: createdUser._id }
5689+
}
5690+
}]);
5691+
}
5692+
catch (err) {
5693+
threw = true;
5694+
assert.equal(err.message, 'Must provide an update object.');
5695+
}
5696+
finally {
5697+
assert.equal(threw, true);
5698+
5699+
const userAfterUpdate = yield User.findOne({ _id: createdUser._id });
5700+
5701+
assert.equal(userAfterUpdate.name, 'Hafez', 'Document data is not wiped if no update object is provided.');
5702+
}
5703+
});
5704+
});
56775705
});
56785706

56795707
it('insertMany with Decimal (gh-5190)', function(done) {

0 commit comments

Comments
 (0)