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.

How to populate Array of Upload with Local API create?

default discord avatar
bazlai2 years ago
2

I have such a collection schema configured



// Models collection schema
const Models: CollectionConfig = {
  slug: 'models',
  fields: [
    {
      name: 'fullname',
      type: 'text',
      required: true,
    },
    {
      name: 'images',
      type: 'array',
      label: 'Images',
      fields: [
        {
          name: 'src',
          type: 'upload',
          relationTo: 'media',
          required: true,
        },
      ]
    },
  ],


But I can't figure out how to populate the images from the Local API create call. I'd really appreciate any help with this.



// Somewhere in the seeding task
type File = {
  data: Buffer;
  mimetype: string;
  name: string;
  size: number;
}

const uploaded: File = await fetchFromUrl(payload, url) // Returns File with Buffer

const model = await payload.create({
  collection: 'models',
  data: {
    fullname: 'Name',
    gender: 'female',
    // Here it fails
    // ↓↓↓↓↓↓
    // ↓↓↓↓↓↓
    images: [{ data: {}, file: uploaded }]
    // ↑↑↑↑↑↑
    // ↑↑↑↑↑↑
  }
})
  • default discord avatar
    user22492 years ago

    I'm having a similar problem.

  • discord user avatar
    jarrod_not_jared
    2 years ago

    You need to create the images in your uploads collection and then pass the id(s) from those created uploads to the field on your models collection.

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.