diff --git a/integration/cloud/main.js b/integration/cloud/main.js index d677209b5..e15437422 100644 --- a/integration/cloud/main.js +++ b/integration/cloud/main.js @@ -7,6 +7,10 @@ Parse.Cloud.define("bar", function(request) { } }); +Parse.Cloud.define('CloudFunctionUndefined', function() { + return undefined; +}); + Parse.Cloud.job('CloudJob1', function() { return { status: 'cloud job completed' diff --git a/integration/test/ParseCloudTest.js b/integration/test/ParseCloudTest.js index 062e12f1b..42770a896 100644 --- a/integration/test/ParseCloudTest.js +++ b/integration/test/ParseCloudTest.js @@ -70,6 +70,13 @@ describe('Parse Cloud', () => { } }); + it('run function with undefined', (done) => { + Parse.Cloud.run('CloudFunctionUndefined', {}).then((result) => { + assert.strictEqual(result, undefined); + done(); + }); + }); + it('run job', (done) => { const params = { startedBy: 'Monty Python' }; Parse.Cloud.startJob('CloudJob1', params).then((jobStatusId) => { diff --git a/src/Cloud.js b/src/Cloud.js index 09d90e5e5..c147a1d53 100644 --- a/src/Cloud.js +++ b/src/Cloud.js @@ -122,14 +122,19 @@ const DefaultController = { ); return request.then((res) => { + if (typeof res === 'object' && + Object.keys(res).length > 0 && + !res.hasOwnProperty('result')) { + throw new ParseError( + ParseError.INVALID_JSON, + 'The server returned an invalid response.' + ); + } const decoded = decode(res); if (decoded && decoded.hasOwnProperty('result')) { return Promise.resolve(decoded.result); } - throw new ParseError( - ParseError.INVALID_JSON, - 'The server returned an invalid response.' - ); + return Promise.resolve(undefined); }); }, diff --git a/src/__tests__/Cloud-test.js b/src/__tests__/Cloud-test.js index b8e083b4c..2a5b9bfda 100644 --- a/src/__tests__/Cloud-test.js +++ b/src/__tests__/Cloud-test.js @@ -162,6 +162,18 @@ describe('CloudController', () => { }); }); + it('run undefined response', (done) => { + const request = jest.fn(); + request.mockReturnValue(Promise.resolve(undefined)); + + const ajax = jest.fn(); + CoreManager.setRESTController({ request: request, ajax: ajax }); + + Cloud.run('myfunction').then(() => { + done(); + }); + }); + it('startJob passes encoded requests', () => { Cloud.startJob('myJob', { value: 12, when: new Date(Date.UTC(2015,0,1)) });