Skip to content

Commit c5b3ea2

Browse files
percypyandplewis
authored andcommitted
Throw error when fetching an object without ID (parse-community#1066)
* Add failing test - Error should be thrown when fetching with an empty string as object ID * Fix failing test by throwing when target has no ID
1 parent b1c1c09 commit c5b3ea2

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/ParseObject.js

+6
Original file line numberDiff line numberDiff line change
@@ -2121,6 +2121,12 @@ const DefaultController = {
21212121
if (options && options.include) {
21222122
params.include = options.include.join();
21232123
}
2124+
if (!target.id) {
2125+
return Promise.reject(new ParseError(
2126+
ParseError.MISSING_OBJECT_ID,
2127+
'Object does not have an ID'
2128+
));
2129+
}
21242130
return RESTController.request(
21252131
'GET',
21262132
'classes/' + target.className + '/' + target._getId(),

src/__tests__/ParseObject-test.js

+20
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,26 @@ describe('ParseObject', () => {
14111411
});
14121412
});
14131413

1414+
it('throw for fetch with empty string as ID', async () => {
1415+
expect.assertions(1);
1416+
CoreManager.getRESTController()._setXHR(
1417+
mockXHR([{
1418+
status: 200,
1419+
response: {
1420+
count: 10
1421+
}
1422+
}])
1423+
);
1424+
const p = new ParseObject('Person');
1425+
p.id = '';
1426+
await expect(p.fetch())
1427+
.rejects
1428+
.toThrowError(new ParseError(
1429+
ParseError.MISSING_OBJECT_ID,
1430+
'Object does not have an ID'
1431+
));
1432+
});
1433+
14141434
it('should fail on invalid date', (done) => {
14151435
const obj = new ParseObject('Item');
14161436
obj.set('when', new Date(Date.parse(null)));

0 commit comments

Comments
 (0)