Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 27 additions & 0 deletions spec/RestQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,31 @@ describe('RestQuery.each', () => {
expect(resultsOne.length).toBe(1);
expect(resultsTwo.length).toBe(1);
});

it('test afterSave response object is return', done => {
Parse.Cloud.beforeSave('TestObject2', function(req) {
req.object.set('tobeaddbefore', true);
req.object.set('tobeaddbeforeandremoveafter', true);
});

Parse.Cloud.afterSave('TestObject2', function(req) {
const jsonObject = req.object.toJSON();
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 it is not so straightforward for others users to use this capability. Can you test just using the code below?

req.object.unset('todelete');
req.object.set('toadd', true);
return req.object;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's actually an other issue I had. If you do that you will have :

{
  "tokeep": true,
  "createdAt": "2019-07-18T09:12:58.496Z",
  "todelete": {
    "__op": "Delete"
  },
  "updatedAt": "2019-07-18T09:12:58.496Z",
  "toadd": true,
  "objectId": "zC2AmMvGxM"
}

the __op is not really great response :/

Copy link
Member

Choose a reason for hiding this comment

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

Can you try like this?

req.object.unset('todelete');
req.object.set('toadd', true);
return req.object._toFullJSON();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With that I have :

{ todelete: { __op: 'Delete' },
     tokeep: true,
     createdAt: '2019-07-25T13:49:32.552Z',
     updatedAt: '2019-07-25T13:49:32.552Z',
     toadd: true,
     objectId: '4lgUT2xWo5',
     __type: 'Object',
     className: 'TestObject2' }

with toJson :

{ todelete: { __op: 'Delete' },
     tokeep: true,
     createdAt: '2019-07-25T13:49:32.552Z',
     updatedAt: '2019-07-25T13:49:32.552Z',
     toadd: true,
     objectId: '4lgUT2xWo5' }

delete jsonObject.todelete;
delete jsonObject.tobeaddbeforeandremoveafter;
jsonObject.toadd = true;

return jsonObject;
});

rest
.create(config, nobody, 'TestObject2', { todelete: true, tokeep: true })
.then(response => {
expect(response.response.toadd).toBeTruthy();
expect(response.response.tokeep).toBeTruthy();
expect(response.response.tobeaddbefore).toBeTruthy();
expect(response.response.tobeaddbeforeandremoveafter).toBeUndefined();
expect(response.response.todelete).toBeUndefined();
done();
});
});
});
5 changes: 5 additions & 0 deletions src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,11 @@ RestWrite.prototype.runAfterSaveTrigger = function() {
this.config,
this.context
)
.then(result => {
if (result && typeof result === 'object') {
this.response.response = result;
}
})
.catch(function(err) {
logger.warn('afterSave caught an error', err);
});
Expand Down
10 changes: 10 additions & 0 deletions src/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ export function getResponseObject(request, resolve, reject) {
) {
return resolve(response);
}
if (
response &&
typeof response === 'object' &&
request.triggerName === Types.afterSave
) {
return resolve(response);
}
if (request.triggerName === Types.afterSave) {
return resolve();
}
response = {};
if (request.triggerName === Types.beforeSave) {
response['object'] = request.object._getSaveJSON();
Expand Down