Skip to content

Commit a5f8dd8

Browse files
authored
Merge pull request #13571 from Automattic/vkarpov15/gh-13570
fix(query): throw readable error when executing a Query instance without an associated model
2 parents aa5c2c2 + eeed3f5 commit a5f8dd8

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

lib/query.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4360,7 +4360,10 @@ Query.prototype.exec = async function exec(op) {
43604360
}
43614361

43624362
if (this.op == null) {
4363-
throw new Error('Query must have `op` before executing');
4363+
throw new MongooseError('Query must have `op` before executing');
4364+
}
4365+
if (this.model == null) {
4366+
throw new MongooseError('Query must have an associated model before executing');
43644367
}
43654368
this._validateOp();
43664369

@@ -4440,6 +4443,13 @@ function _executePostExecHooks(query) {
44404443
*/
44414444

44424445
function _executePostHooks(query, res, error, op) {
4446+
if (query._queryMiddleware == null) {
4447+
if (error != null) {
4448+
throw error;
4449+
}
4450+
return res;
4451+
}
4452+
44434453
return new Promise((resolve, reject) => {
44444454
const opts = error ? { error } : {};
44454455

@@ -4473,6 +4483,10 @@ function _executePreExecHooks(query) {
44734483
*/
44744484

44754485
function _executePreHooks(query, op) {
4486+
if (query._queryMiddleware == null) {
4487+
return;
4488+
}
4489+
44764490
return new Promise((resolve, reject) => {
44774491
query._queryMiddleware.execPre(op || query.op, query, [], (error) => {
44784492
if (error != null) {

test/query.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4094,6 +4094,7 @@ describe('Query', function() {
40944094
await Test.findOneAndUpdate({}, { name: 'bar' });
40954095
assert.ok(!('projection' in lastOptions));
40964096
});
4097+
40974098
it('should provide a clearer error message when sorting with empty string', async function() {
40984099
const testSchema = new Schema({
40994100
name: { type: String }
@@ -4104,6 +4105,32 @@ describe('Query', function() {
41044105
await Error.find().sort('-');
41054106
}, { message: 'Invalid field "" passed to sort()' });
41064107
});
4108+
4109+
it('throws a readable error when executing Query instance without a model (gh-13570)', async function() {
4110+
const schema = new Schema({ name: String });
4111+
const M = db.model('Test', schema, 'Test');
4112+
await M.deleteMany({});
4113+
await M.create({ name: 'gh13570' });
4114+
4115+
const Q = new mongoose.Query();
4116+
await assert.rejects(
4117+
() => Q.collection('Test').find().lean(),
4118+
/Query must have an associated model before executing/
4119+
);
4120+
});
4121+
4122+
it('throws a readable error when executing Query instance without an op (gh-13570)', async function() {
4123+
const schema = new Schema({ name: String });
4124+
const M = db.model('Test', schema, 'Test');
4125+
await M.deleteMany({});
4126+
await M.create({ name: 'gh13570' });
4127+
4128+
const Q = new M.Query();
4129+
await assert.rejects(
4130+
() => Q.lean(),
4131+
/Query must have `op` before executing/
4132+
);
4133+
});
41074134
it('converts findOneAndUpdate to findOneAndReplace if overwrite set (gh-13550)', async function() {
41084135
const testSchema = new Schema({
41094136
name: { type: String }

0 commit comments

Comments
 (0)