How to update current document in afterRead hook? #1764
-
Merry Christmas to you all! Here is a problem I currently have: Setup: const MyGlobal: GlobalConfig = {
slug: 'my-global',
fields: [
{
name: 'apiRequestKey',
type: 'text',
hooks: {
beforeChange: [populateData],
afterRead: [populateData],
},
}
{
name: 'updatedAt',
type: 'date',
admin: {
hidden: true,
},
},
{
name: 'apiData',
type: 'number',
admin: {
hidden: true,
},
},
]
} Inside the hook, I check if updatedAt is expired, and refetch the apiData from an external API. Now if I try to use the local API with Possible solutions would be:
Does anybody have an idea for an elegant solution to this problem? Any help is appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I solved it for now using an early return with
However, I would still be interested in suggestions for other solutions, since the potential for an endless loop is still present, if the data changes in a specific way. I would still prefer to be able to disable hooks on the update call. |
Beta Was this translation helpful? Give feedback.
-
In case anyone runs into this, you can use context https://payloadcms.com/docs/hooks/context |
Beta Was this translation helpful? Give feedback.
I solved it for now using an early return with
However, I would still be interested in suggestions for other solutions, since the potential for an endless loop is still present, if the data changes in a specific way. I would still prefer to be able to disable hooks on the update call.