I'm trying to build a plugin for Payload that provides hooks for collection.
Problem is that I'm using local API inside hooks and when trying to access
payload.collections
it returns an empty object
payload
object was imported like
import payload from 'payload'
How can I use local API inside plugin?
Payload needs to be initialized before you can use its local API - so when your plugin instantiates, that is
beforePayload becomes fully initialized
you should use the
onInit
method of the config instead if you need to interact with the local API inside of a plugin
So I can make function for onInit that will save reference to payload and use that reference instead?
payload
comes through as an argument to that function
yes, but I need to use payload in other functions, that were added to collections as hooks before onInit
that's fine too
because when those hooks are actually executed, payload will
have been already initialized
note that you can also get payload off
req.payload
where
req
is an arg to hooks
thats actualy better
Yep, that helped, thank you!
Star
Discord
online
Get dedicated engineering support directly from the Payload team..