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.

req.collection is undefined for AfterRead in FIELD HOOK (not collection hook)

default discord avatar
bombnp2 years ago
2

I'm writing a custom field with an AfterRead field hook (not collection hook), and I want to access the collection this hook is being run from. When I try to access

req.collection

from the hook's arguments, it's undefined. When I try using collection hooks instead, the

req.collection

is NOT undefined as expected.



Is there a way to access the collection in field hooks?

  • discord user avatar
    alessiogr
    2 years ago

    Looking into this right now!



    @275258786388639744

    Hm I cannot reproduce it. Tried adding an afterRead hook to a field, and

    req.collection

    works perfectly.



    Could you try creating a minimal reproduction using this guide:

    https://github.com/payloadcms/payload/blob/master/.github/reproduction-guide.md

    ?



    Specifically this is what I tried, and req.collection does exist here

  • default discord avatar
    bombnp2 years ago

    Wth I tried to create a minimal reproduction repo with the same code and it works fine.



    Then, I went back to my own project with the same code and it somehow works fine now without any changes. Maybe some shenanigans with nodemon i guess. Sorry for wasting your time

    :1019870478233518121:
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.