Skip to content

Commit d8f3fb1

Browse files
authored
Skip afterFind for Aggregate and Distinct Queries (#4596)
1 parent 213801c commit d8f3fb1

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

spec/CloudCode.spec.js

+42
Original file line numberDiff line numberDiff line change
@@ -1819,4 +1819,46 @@ describe('afterFind hooks', () => {
18191819
Parse.Cloud.afterSave('_PushStatus', () => {});
18201820
}).not.toThrow();
18211821
});
1822+
1823+
it('should skip afterFind hooks for aggregate', (done) => {
1824+
const hook = {
1825+
method: function() {
1826+
return Promise.reject();
1827+
}
1828+
};
1829+
spyOn(hook, 'method').and.callThrough();
1830+
Parse.Cloud.afterFind('MyObject', hook.method);
1831+
const obj = new Parse.Object('MyObject')
1832+
const pipeline = [{
1833+
group: { objectId: {} }
1834+
}];
1835+
obj.save().then(() => {
1836+
const query = new Parse.Query('MyObject');
1837+
return query.aggregate(pipeline);
1838+
}).then((results) => {
1839+
expect(results[0].objectId).toEqual(null);
1840+
expect(hook.method).not.toHaveBeenCalled();
1841+
done();
1842+
});
1843+
});
1844+
1845+
it('should skip afterFind hooks for distinct', (done) => {
1846+
const hook = {
1847+
method: function() {
1848+
return Promise.reject();
1849+
}
1850+
};
1851+
spyOn(hook, 'method').and.callThrough();
1852+
Parse.Cloud.afterFind('MyObject', hook.method);
1853+
const obj = new Parse.Object('MyObject')
1854+
obj.set('score', 10);
1855+
obj.save().then(() => {
1856+
const query = new Parse.Query('MyObject');
1857+
return query.distinct('score');
1858+
}).then((results) => {
1859+
expect(results[0]).toEqual(10);
1860+
expect(hook.method).not.toHaveBeenCalled();
1861+
done();
1862+
});
1863+
});
18221864
});

src/RestQuery.js

+4
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,10 @@ RestQuery.prototype.runAfterFindTrigger = function() {
584584
if (!hasAfterFindHook) {
585585
return Promise.resolve();
586586
}
587+
// Skip Aggregate and Distinct Queries
588+
if (this.findOptions.pipeline || this.findOptions.distinct) {
589+
return Promise.resolve();
590+
}
587591
// Run afterFind trigger and set the new results
588592
return triggers.maybeRunAfterFindTrigger(triggers.Types.afterFind, this.auth, this.className,this.response.results, this.config).then((results) => {
589593
this.response.results = results;

0 commit comments

Comments
 (0)