Skip to content

Commit 0cc4978

Browse files
dplewisflovilmart
authored andcommitted
Handle undefined in Cloud Code (#682)
* Handle undefined in Cloud Code * properly handle undefined
1 parent 2e69adf commit 0cc4978

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

integration/cloud/main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ Parse.Cloud.define("bar", function(request) {
77
}
88
});
99

10+
Parse.Cloud.define('CloudFunctionUndefined', function() {
11+
return undefined;
12+
});
13+
1014
Parse.Cloud.job('CloudJob1', function() {
1115
return {
1216
status: 'cloud job completed'

integration/test/ParseCloudTest.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ describe('Parse Cloud', () => {
7070
}
7171
});
7272

73+
it('run function with undefined', (done) => {
74+
Parse.Cloud.run('CloudFunctionUndefined', {}).then((result) => {
75+
assert.strictEqual(result, undefined);
76+
done();
77+
});
78+
});
79+
7380
it('run job', (done) => {
7481
const params = { startedBy: 'Monty Python' };
7582
Parse.Cloud.startJob('CloudJob1', params).then((jobStatusId) => {

src/Cloud.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,19 @@ const DefaultController = {
122122
);
123123

124124
return request.then((res) => {
125+
if (typeof res === 'object' &&
126+
Object.keys(res).length > 0 &&
127+
!res.hasOwnProperty('result')) {
128+
throw new ParseError(
129+
ParseError.INVALID_JSON,
130+
'The server returned an invalid response.'
131+
);
132+
}
125133
const decoded = decode(res);
126134
if (decoded && decoded.hasOwnProperty('result')) {
127135
return Promise.resolve(decoded.result);
128136
}
129-
throw new ParseError(
130-
ParseError.INVALID_JSON,
131-
'The server returned an invalid response.'
132-
);
137+
return Promise.resolve(undefined);
133138
});
134139
},
135140

src/__tests__/Cloud-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,18 @@ describe('CloudController', () => {
162162
});
163163
});
164164

165+
it('run undefined response', (done) => {
166+
const request = jest.fn();
167+
request.mockReturnValue(Promise.resolve(undefined));
168+
169+
const ajax = jest.fn();
170+
CoreManager.setRESTController({ request: request, ajax: ajax });
171+
172+
Cloud.run('myfunction').then(() => {
173+
done();
174+
});
175+
});
176+
165177
it('startJob passes encoded requests', () => {
166178
Cloud.startJob('myJob', { value: 12, when: new Date(Date.UTC(2015,0,1)) });
167179

0 commit comments

Comments
 (0)