Using Local API inside plugin

default discord avatar
Ysionelast year
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
    last year

    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
    Ysionelast year

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

  • discord user avatar
    jmikrut
    last year
    payload

    comes through as an argument to that function

  • default discord avatar
    Ysionelast year

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

  • discord user avatar
    jmikrut
    last year

    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
    Ysionelast year

    thats actualy better



    Yep, that helped, thank you!

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

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