Hello!
I'm on payload
1.10.1
In a
CollectionAfterChangeHook
, TS is telling me that
updatedDoc.someRelationField: string | RelatedCollection
, so I have to add conditionals for this
But, I can't think of a scenario where the
updatedDoc
would come through with
depth > 0
.
Can I safely assume that
depth=0
in this hook to avoid an
if
statement? I would prefer to do something like
updatedDoc.someRelationshipField as string
if we can assume this
@allannnc Good morning!
I believe it can be a string or related collection, because if the depth is specified to shallow, the document wont be returned, only the document ID (string)
I think your
as
solution is acceptable in this case, as the types are to help you as a tool but you have the final say
Thanks for the answer!
The catch is I'm not sure what the final say is though - it boils down to how does this get populated?
Good question! I'm not 100% sure, but some other members here may be able to provide some details.
What do you mean by populated?
Yaknow, req comes in, docs get attached
It gets populated in the afterRead field hook (before the collection afterChange hook), which takes into account
depth
. So if you run a payload.update and use a depth param, the docs may or may not be populated depending on depth number
@jarrod_not_jared I just stumbled on this and there does appear to be some default depth in these hooks on an
payload.update
in my testing I was seeing nested relationships fully available in a collection
afterChange
hook, even when I specify no
depth
is that expected behavior?
I believe it is 1 by default
hmm def seeing deeper than that
looks like 2
I used a recursive relationship to test
But I feel like the point still stands, if you pass depth: 0 inside the payload.update, then the type will be string
cool
I've been running into a similar/related issue quite regularly, that definitely seems like a bug. I opened an issue on github:
https://github.com/payloadcms/payload/discussions/3218. The depth/format of the doc argument of several of my CollectionAfterChangeHook's differs depending on whether the "change" was caused by a manual user action or a programatic change (such as another hook). I am not setting the depth in any of my payload.update/payload.create calls that would effect the hooks.
Yea I'm finding this is breaking some of our filters and related functionality, but it's been very hard to pinpoint the issue
Star
Discord
online
Get dedicated engineering support directly from the Payload team..