Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

after read hook - infinite loop

default discord avatar
binarybeast.last year
3

Hey guys


I have a "movies" collection, and I field called "related movies'. When returning the movie, I want to populate the "related movies' field. So, I used afterreadhook where I make a call to "movies" collection to get 5 random movies. Seems like afterread hook got triggered for other movies as well and I ended up in the infinite loop. Is there a way to stop it after the first time?

  • default discord avatar
    notchrlast year

    Morning! Can we see your logic please?

  • default discord avatar
    eustachi0last year

    It seems like passing

    context

    to the afterChange hook will help here. You can check the documentation, the first link

    Context - When to use Context
  • default discord avatar
    binarybeast.last year

    Yeah, thank you guys. Made it work. If someone needs reference -

    https://payloadcms.com/docs/hooks/context#preventing-infinite-loops
Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.