How to populate Array of Upload with Local API create?

default discord avatar
BazZy
2 months 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
    user2249
    last month

    I'm having a similar problem.

  • discord user avatar
    Jarrod
    Payload Team
    last month

    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.

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.