Skip to content

Commit 519a838

Browse files
committed
add unset in afterSave
1 parent fb0ae8e commit 519a838

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

spec/CloudCode.spec.js

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,8 +1495,6 @@ describe('Cloud Code', () => {
14951495
});
14961496

14971497
it('before save can revert fields', async () => {
1498-
await reconfigureServer({ silent: false });
1499-
15001498
Parse.Cloud.beforeSave('TestObject', ({ object }) => {
15011499
object.revert('foo');
15021500
return object;
@@ -1518,8 +1516,6 @@ describe('Cloud Code', () => {
15181516
});
15191517

15201518
it('before save can revert fields with existing object', async () => {
1521-
await reconfigureServer({ silent: false });
1522-
15231519
Parse.Cloud.beforeSave(
15241520
'TestObject',
15251521
({ object }) => {
@@ -1552,6 +1548,43 @@ describe('Cloud Code', () => {
15521548
expect(obj.get('foo')).toBe('bar');
15531549
});
15541550

1551+
it('can unset in afterSave', async () => {
1552+
Parse.Cloud.beforeSave('TestObject', ({ object }) => {
1553+
if (!object.existed()) {
1554+
object.set('secret', true);
1555+
}
1556+
return object;
1557+
});
1558+
1559+
Parse.Cloud.afterSave(
1560+
'TestObject',
1561+
({ object }) => {
1562+
object.unset('secret');
1563+
},
1564+
{
1565+
skipWithMasterKey: true,
1566+
}
1567+
);
1568+
1569+
Parse.Cloud.afterFind(
1570+
'TestObject',
1571+
({ objects }) => {
1572+
return objects.map(object => object.unset('secret'));
1573+
},
1574+
{
1575+
skipWithMasterKey: true,
1576+
}
1577+
);
1578+
1579+
const obj = new TestObject();
1580+
await obj.save();
1581+
expect(obj.get('secret')).toBeUndefined();
1582+
await obj.fetch();
1583+
expect(obj.get('secret')).toBeUndefined();
1584+
await obj.fetch({ useMasterKey: true });
1585+
expect(obj.get('secret')).toBe(true);
1586+
});
1587+
15551588
it('should revert in beforeSave', async () => {
15561589
Parse.Cloud.beforeSave('MyObject', ({ object }) => {
15571590
if (!object.existed()) {

src/RestWrite.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,9 +1536,8 @@ RestWrite.prototype.runAfterSaveTrigger = function () {
15361536
this.context
15371537
)
15381538
.then(result => {
1539-
if (result && typeof result === 'object') {
1540-
this.response.response = this._updateResponseWithData(result._toFullJSON(), this.data);
1541-
}
1539+
const object = result && typeof result === 'object' ? result : updatedObject;
1540+
this.response.response = this._updateResponseWithData(object._toFullJSON(), this.data);
15421541
})
15431542
.catch(function (err) {
15441543
logger.warn('afterSave caught an error', err);

0 commit comments

Comments
 (0)