Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Can I populate a Select field based on an API call made when editing an item in a collection?

default discord avatar
notchr2 years ago
1

I have a "Users" collection where every user has an additional internal ID. I want to make a call to an external API with that ID and populate the items of a select field based on the response. I think this is do-able, but I'm blanking on how 😄



I'm thinking of using afterRead?



    {
      name: 'events',
      maxDepth: 0,
      type: 'relationship',
      relationTo: 'events',
      hasMany: true,
      access: {
        create: () => false,
        update: () => false,
      },
      admin: {
        readOnly: true,
      },
      hooks: {
        beforeChange: [({ siblingData }) => {
          // Mutate the sibling data to prevent DB storage
          // eslint-disable-next-line no-param-reassign
          siblingData.events = undefined;
        }],
        afterRead: [getAllEvents],
      },
    },


But im not sure why we need to mutate the sibling data



or if that was only for that example



Hmmm that runs on every item in the collection



Was hoping for it to run only during editing



@858693520012476436

Do you think I'm going about it incorrectly?

  • default discord avatar
    paulpopus2 years ago

    i cant test rn but im pretty sure in those functions you have access to the full

    req

    from express



    and im pretty sure that object contains something related to what API is being used/invoked and if it's admin panel?



    i cant recall right now, can look later for you

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.