Replies: 3 comments 9 replies
-
If your field hooks depend on values of other fields that are themselves modified by their own field hooks, it might be wiser to merge all that logic into a collection level hook so you have full control of the order of execution and can edit all fields at the same place. |
Beta Was this translation helpful? Give feedback.
-
That is a solution, though for my specific implementation this is occurring on an importable field group, what you're suggesting would mean importing the field group and setting up a collection hook on every collection that uses this which feels a bit brittle and not properly encapsulated. That said I'd still expect inter-hook behavior to be deterministic, this still feels like a bug to me. |
Beta Was this translation helpful? Give feedback.
-
Let me chime in here quick with some additional info. Field hooks are intentionally all run in parallel to one another for each step. So your This is a performance optimization as well as a side-effect of the way that JavaScript works. The properties of field data are just keys of a plain old JavaScript object, which do not have intrinsic / reliable order. So even though we do map through the properties of your field data, the order is not predictable. If you do need predictability in certain fields' calculations, then I would view the different hooks as the only reliable way to guarantee hook order. Or, as @swenzel-arc mentioned, just use a collection hook instead. What would the alternative be here? I am certainly not opposed to building some type of "field hook ordering" system, but that would be more of a new feature and less of an issue with Payload as it stands now. What do you think? I'll convert this to a discussion but if I have misunderstood something, and there is indeed a bug with Payload here, I'd be happy to re-open as an issue. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Link to reproduction
N/A
To Reproduce
This is a fairly complex set up and I haven't been able to reproduce it in a trivial example so I'm hoping something jumps out to whoever reviews this. I've encountered this bug a number of times over the last few months and because it's non-deterministic it's very hard to reliably reproduce.
Describe the Bug
I have two examples of this, one was where I setting a
console.log
within a beforeValidate hook would change the outcome of the validation (see this discord thread: https://discord.com/channels/967097582721572934/1091785894136139808/1091785894136139808).More recently I've lost a few hours trying to debug this beforeChange hook and similarly hooks are firing and data is available non-deterministically:
Some important context:
slug
is being set in abeforeValidate
hookparent
is being set by a async method ondefaultValue
This seems to reliably work in the admin UI, but never works on a local API
create
. When setting breakpoints in the above beforeChange hook I see data.slug is set correctly by it's beforeValidate hook, but data.parent is undefined.FWIW I've tried different combos of beforeChange, beforeValidate, afterChange to address this and nothing seems to reliably solve it.
One important data point, adding a sleep completely solves this which makes me pretty confident it's a race condition:
Payload Version
1.8.3 (but have noticed this since 1.6.x)
Beta Was this translation helpful? Give feedback.
All reactions