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

default discord avatar
bombnp
3 months ago
4

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
    Payload Team
    3 months ago

    Looking into this right now!



    @bombnp 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

    Screenshot_2023-06-10_at_14.32.292x.jpg
  • default discord avatar
    bombnp
    3 months 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:
Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.