Replies: 5 comments 3 replies
-
Hey @wikoci — this is a pretty common problem with many dev tasks. Generally we'd say the right way to do this would be to store some type of flag that lets you know that whatever the hook was needed for has been done, so you can not update the document again. But, if you wanted to avoid triggering hooks, you could access the Mongoose model directly, and update whatever you need directly with the Mongoose model. You can access Mongoose models by Another way would be to bind something to the One of the options above should work for you. I'm going to convert this to a discussion because it's not necessarily something that Payload should tackle, moreso just a good discussion about how to avoid infinite loops with hooks. Very good question! 👍 |
Beta Was this translation helpful? Give feedback.
-
For anyone who ends up here, I was able to accomplish this with a
|
Beta Was this translation helpful? Give feedback.
-
Not sure if this is a bug or not but you can tell that the hook has started to be in the loop if 'req.user' is already falsy. |
Beta Was this translation helpful? Give feedback.
-
We experienced a similar Problem with We have a collection of articles and want to add a When we use We worked around this using a flag outside of the hook, that is set in the initial hook call, and cleaned up after the first find (every subsequent find is therefore skipped). The direct access of the mongoose model would also be a workaround for this, but the I would've proposed adding an option like I'm open to contribute on a PR for a feature like that if that is something you are interested in. |
Beta Was this translation helpful? Give feedback.
-
I'm encoutering this problem quite often. And the proposed solutions work, unfortunatelly none of them is clean (e.g. flag on req throws typing err and it force you to actually pass req object to all subsequent Local API calls which can alter something you do not wish (user object, access, locale) and also is not possible to pass to some Local API calls like typed payload.update; and mongoose way doesn't trigger any hook at all which is not good if you need to skip or alter only one hook). This gets painful as you get more complex hooks flow. I propose following solution:adding a new property to Local API actions |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Do you have any options to bypass all hooks when using the local API.
In order to avoid loop in hooks between two collections or fields.
Example :
A-Collection
-Hook afterChange update something in CB
B-Collection
update something in CA.
Beta Was this translation helpful? Give feedback.
All reactions