-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Properly handle return values in beforeSave #5228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Properly handle return values in beforeSave #5228
Conversation
a possible bug found where beforeSave does not apply changes to request object if the beforeSave hook ends with 'true' returned
Awesome! Thanks! Tell me, would you be tempted to attempt a fix? |
I leveled up and I just got a quest! Sure, I’ll give it a try 😊 |
Thanks @blastoy! |
OK, I've narrowed down the problem to this particular area: Lines 255 to 266 in a4b592a
In the case where I add In the case where I don't add What is the intent of the first if statement? You can see some of the effects of what happens when I write That Lines 527 to 544 in a4b592a
That Lines 233 to 251 in a4b592a
|
Codecov Report
@@ Coverage Diff @@
## master #5228 +/- ##
=========================================
- Coverage 93.93% 93.9% -0.04%
=========================================
Files 123 123
Lines 8975 9012 +37
=========================================
+ Hits 8431 8463 +32
- Misses 544 549 +5
Continue to review full report at Codecov.
|
also changed test cases to be more descriptive + added extra test case that returns promise in the beforeSave
My solution to the bug was to add some code right after the beforeSave trigger finishes that ignores whatever the hook returned. Figured if beforeSave works when the hook returns nothing, then making sure returns are ignored would work. Let me know what you guys think! |
You have some failing tests. Please check Travis. |
@blastoy Ping? |
@blastoy Sorry for the late reply. Without looking into it, since you did change the functionality of the beforeSave, some of the beforeSave tests are failing. Update those tests to match your changes. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
@acinader I fixed the PR to address the original issue. It was discovered that returning in beforeSave was unnecessary and could lead to the hook not working. I can update the docs to address this. |
this doesn't make sense to me.
|
This works fine, the issue is if the promise succeeds. This works
This works
This fails
Anything returned in the promise will be passed along as a response which prevents req.object from being passed. |
the failing case is legal though. and in the case where you're calling a function that may reject or may resolve, then what? I suspect that @blastoy had it right with "That response in the then is just a raw Boolean true, response.object being undefined. No fieldsChangedByTrigger will be found so its [sic] like no changes were done to the object" If I've got it right, there are two options to solve:
|
This reverts commit e01c57d.
prevent updating the object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
I added a test to confirm that if you reject it won't change values. I couldn't find a test that did that and I looked!
If it all looks good to you, merge away.
Good catch on that test! @blastoy Thanks for the PR! |
* 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 e01c57d. * fix promises and tests * Add a test to verify that a failed beforeChange hook will prevent updating the object.
Closes: #5227
a possible bug found where beforeSave does not apply changes to request
object if the beforeSave hook ends with 'true' and promises returned