Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix findByIdAndUpdate behavior with undefined id #11079

Merged
merged 6 commits into from
Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2621,6 +2621,10 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
* A.findByIdAndUpdate() // returns Query
*
* ####Note:
*
* If id is undefined and update is specified, an error will be thrown.
*
* ####Note:
*
* All top level update keys which are not `atomic` operation names are treated as set operations:
*
Expand Down Expand Up @@ -2676,6 +2680,9 @@ Model.findByIdAndUpdate = function(id, update, options, callback) {
throw new TypeError(msg);
}
return this.findOneAndUpdate({ _id: id }, undefined);
} else if (id === undefined) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our tests rely on Model.findByIdAndUpdate() with no arguments working, so can we make this else if (arguments.length > 0 && id === undefined)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for bringing this up. I forgot to consider that. I went ahead and added this to the condition

const msg = 'Model.findByIdAndUpdate(): id cannot be undefined if update is specified.\n';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why \n?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I was just following the format of the preceding error messages. I went ahead and removed it

throw new TypeError(msg);
}

// if a model is passed in instead of an id
Expand Down
16 changes: 16 additions & 0 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,22 @@ describe('model: populate:', function() {
});
});

it ('fail on undefined id update', function(done) {
const BlogPost = db.model('BlogPost', blogPostSchema);

BlogPost.create(
{ title: 'woot' },
function(err, post) {
assert.ifError(err);

BlogPost.
findByIdAndUpdate(undefined, { $set: { _creator: {} } }, function(err) {
assert.ok(err);
done();
});
});
})

it('across DBs', function(done) {
const db = start();
const db2 = db.useDb('mongoose_test2');
Expand Down