Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions integration/test/ParseObjectTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ describe('Parse Object', () => {
});
});

it('can check if object exists', async () => {
const object = new TestObject();
await object.save();
assert.equal(await object.exists(), true);
await object.destroy();
assert.equal(await object.exists(), false);
});

it('can find objects', (done) => {
const object = new TestObject({ foo: 'bar' });
object.save().then(() => {
Expand Down
21 changes: 21 additions & 0 deletions src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,27 @@ class ParseObject {
return false;
}

/**
* Returns true if this object exists on the Server
*
* @param {Object} options
* Valid options are:<ul>
* <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
* be used for this request.
* <li>sessionToken: A valid session token, used for making a request on
* behalf of a specific user.
* </ul>
* @return {Promise<boolean>} A boolean promise that is fulfilled if object exists.
*/
async exists(options?: RequestOptions): Promise<boolean> {
try {
await this.fetch(options);
return true;
} catch (e) {
return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should check e.code === 101 and only return false in this case. Otherwise throw e. I am afraid that a network issue could be interpreted as an inexistent object.

Copy link
Member

@davimacedo davimacedo Aug 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking here again. Maybe do a query by id would be better here instead of the fetch? I am not sure if the developer, when calling an exists() function, is expecting the object data to be updated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats a good point.

}
}

/**
* Checks if the model is currently in a valid state.
* @return {Boolean}
Expand Down
16 changes: 16 additions & 0 deletions src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,22 @@ describe('ParseObject', () => {
spy.mockRestore();
});

it('can check if object exists', async () => {
const objectController = CoreManager.getObjectController();
const spy = jest.spyOn(
objectController,
'fetch'
)
.mockImplementationOnce(() => Promise.resolve())
.mockImplementationOnce(() => Promise.reject());

const parent = new ParseObject('Person');
expect(await parent.exists()).toBe(true);
expect(await parent.exists()).toBe(false);

spy.mockRestore();
});

it('can save the object', (done) => {
CoreManager.getRESTController()._setXHR(
mockXHR([{
Expand Down