I have a collection called 'investments', and one of the properties is a 'project', which is a relationship. When I use the afterChange hook, in the doc parameter I'm receiving the project id instead of the full investment document. What could be the problem?
@jmikrut
I've been facing a similar problem recently. For me, it was that depending on whether I submit from the admin panel or the local api, I either receive the full doc or just the ID.
It's because the admin panel uses depth 0 while local api uses depth 3. I wrote a workaround to handle all cases,
if (!order.user?.id) {
const user = await payload.findByID({
collection: 'users',
id: order.user
})
returnObj.populated.user = user
} else {
returnObj.populated.user = order.user
}
This creates a "populated" attribute that always contains the full doc data. since the order doesn't have an attribute with name "populated", it doesn't cause any issues. Idk if that's the prettiest solution but it works for me.
Note that in my case, the user field is required, so I don't have to handle the case of order.user being undefined
Thanks @Sandro Wegmann ! But in my case, the ID I'm receiving is not even the ID of the target document, it's actually an ID of a relationship from a property inside de document. So I can't even manually find it using the received ID
So in an afterChange Hook of investments, you only receive the project ID and nothing else?
Exactly
weird
I'm updating the payload to see if I manage to change that
Any hooks before this one?
What you return from a hook becomes the doc param in the next hook
hmm, that sounds accurate
let me check what I'm returning
I have indeed a beforeChange
oh looks like that's the problem, I'm just having a problem with the router here because of the update
fixing that I'll check if it's working
I appreciate the help
apparently It could even be the afterDelete hook to be honest, that's the one that could send the project id
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.