Available fields in hooks?

default discord avatar
Derosul
last week
1

I'm trying to create a generic hook for setting a default value on a field on save, but whenever someone saves the document, the field key is not there when the field has not been filled in by the user.



Would it possible to to get the available fields for a collection while in a hook?



Well, I think I answered my own question. For future reference:



You can get the

req

object in a hook, which includes a

collection

key



So you can access the configured fields like this:

req.collection.config.fields
    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.