Properly handle return values in beforeSave (#5228)
* added failing test case to CloudCode.spec.js a possible bug found where beforeSave does not apply changes to request object if the beforeSave hook ends with 'true' returned * moddified triggers to return null when beforeSave also changed test cases to be more descriptive + added extra test case that returns promise in the beforeSave * address original issue * Revert "address original issue" This reverts commit e01c57d1de5c4b2fe21e9ebd590211d21330cdda. * fix promises and tests * Add a test to verify that a failed beforeChange hook will prevent updating the object.
This commit is contained in:
committed by
Diamond Lewis
parent
8622e5c378
commit
bf033becbd
@@ -254,6 +254,7 @@ export function getResponseObject(request, resolve, reject) {
|
||||
// Use the JSON response
|
||||
if (
|
||||
response &&
|
||||
typeof response === 'object' &&
|
||||
!request.object.equals(response) &&
|
||||
request.triggerName === Types.beforeSave
|
||||
) {
|
||||
@@ -573,6 +574,20 @@ export function maybeRunTrigger(
|
||||
auth
|
||||
);
|
||||
}
|
||||
// beforeSave is expected to return null (nothing)
|
||||
if (triggerType === Types.beforeSave) {
|
||||
if (promise && typeof promise.then === 'function') {
|
||||
return promise.then(response => {
|
||||
// response.object may come from express routing before hook
|
||||
if (response && response.object) {
|
||||
return response;
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
return promise;
|
||||
})
|
||||
.then(success, error);
|
||||
|
||||
Reference in New Issue
Block a user