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

default discord avatar
notchr
4 months 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



@paulpopus Do you think I'm going about it incorrectly?

  • default discord avatar
    paulpopus
    4 months 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

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.