diff --git a/spec/CloudCode.spec.js b/spec/CloudCode.spec.js index 34ce628a34..1efc75f90f 100644 --- a/spec/CloudCode.spec.js +++ b/spec/CloudCode.spec.js @@ -645,4 +645,33 @@ describe('Cloud Code', () => { done(); }); }); + + it('should fully delete objects when using `unset` with beforeSave (regression test for #1840)', done => { + var TestObject = Parse.Object.extend('TestObject'); + var BeforeSaveObject = Parse.Object.extend('BeforeSaveChanged'); + + Parse.Cloud.beforeSave('BeforeSaveChanged', (req, res) => { + var object = req.object; + object.set('before', 'save'); + object.unset('remove'); + res.success(); + }); + + let object; + let testObject = new TestObject({key: 'value'}); + testObject.save().then(() => { + object = new BeforeSaveObject(); + return object.save().then(() => { + object.set({remove:testObject}) + return object.save(); + }); + }).then((objectAgain) => { + expect(objectAgain.get('remove')).toBeUndefined(); + expect(object.get('remove')).toBeUndefined(); + done(); + }).fail((err) => { + console.error(err); + done(); + }) + }); }); diff --git a/src/RestWrite.js b/src/RestWrite.js index bc812d3a1c..acf364719c 100644 --- a/src/RestWrite.js +++ b/src/RestWrite.js @@ -858,7 +858,7 @@ function updateResponseWithData(response, data) { let dataValue = data[fieldName]; let responseValue = response[fieldName]; if (dataValue && dataValue.__op === 'Delete') { - delete response[fieldName]; + response[fieldName] = undefined; } else { response[fieldName] = responseValue || dataValue; }