Using Local API inside plugin

default discord avatar
Ysione
6 months ago
11

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?

  • discord user avatar
    jmikrut
    Payload Team
    6 months ago

    Payload needs to be initialized before you can use its local API - so when your plugin instantiates, that is

    before

    Payload 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

  • default discord avatar
    Ysione
    6 months ago

    So I can make function for onInit that will save reference to payload and use that reference instead?

  • discord user avatar
    jmikrut
    Payload Team
    6 months ago
    payload

    comes through as an argument to that function

  • default discord avatar
    Ysione
    6 months ago

    yes, but I need to use payload in other functions, that were added to collections as hooks before onInit

  • discord user avatar
    jmikrut
    Payload Team
    6 months ago

    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

  • default discord avatar
    Ysione
    6 months ago

    thats actualy better



    Yep, that helped, thank you!

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.