diff --git a/spec/CloudCode.spec.js b/spec/CloudCode.spec.js index 6404b1675f..98e52e056e 100644 --- a/spec/CloudCode.spec.js +++ b/spec/CloudCode.spec.js @@ -1819,4 +1819,46 @@ describe('afterFind hooks', () => { Parse.Cloud.afterSave('_PushStatus', () => {}); }).not.toThrow(); }); + + it('should skip afterFind hooks for aggregate', (done) => { + const hook = { + method: function() { + return Promise.reject(); + } + }; + spyOn(hook, 'method').and.callThrough(); + Parse.Cloud.afterFind('MyObject', hook.method); + const obj = new Parse.Object('MyObject') + const pipeline = [{ + group: { objectId: {} } + }]; + obj.save().then(() => { + const query = new Parse.Query('MyObject'); + return query.aggregate(pipeline); + }).then((results) => { + expect(results[0].objectId).toEqual(null); + expect(hook.method).not.toHaveBeenCalled(); + done(); + }); + }); + + it('should skip afterFind hooks for distinct', (done) => { + const hook = { + method: function() { + return Promise.reject(); + } + }; + spyOn(hook, 'method').and.callThrough(); + Parse.Cloud.afterFind('MyObject', hook.method); + const obj = new Parse.Object('MyObject') + obj.set('score', 10); + obj.save().then(() => { + const query = new Parse.Query('MyObject'); + return query.distinct('score'); + }).then((results) => { + expect(results[0]).toEqual(10); + expect(hook.method).not.toHaveBeenCalled(); + done(); + }); + }); }); diff --git a/src/RestQuery.js b/src/RestQuery.js index 0af910522b..25e5d51323 100644 --- a/src/RestQuery.js +++ b/src/RestQuery.js @@ -584,6 +584,10 @@ RestQuery.prototype.runAfterFindTrigger = function() { if (!hasAfterFindHook) { return Promise.resolve(); } + // Skip Aggregate and Distinct Queries + if (this.findOptions.pipeline || this.findOptions.distinct) { + return Promise.resolve(); + } // Run afterFind trigger and set the new results return triggers.maybeRunAfterFindTrigger(triggers.Types.afterFind, this.auth, this.className,this.response.results, this.config).then((results) => { this.response.results = results;