#Create/update localized fields of array field items with Local API

4 messages · Page 1 of 1 (latest)

candid slate
#

I have a collection with an array field. The array items have a localized text field. E.g.:

    {
      name: 'items',
      type: 'array',
      fields: [
        {
          name: 'title',
          type: 'text',
          localized: true,
        },
      ],
    },

I want to create a new record via Local API with multiple array items and set the text in multiple languages. How can i do that?

I tried first creating the record in the first language and then updating it in the others like this:

  const record = await payload.create({
    collection: 'my-collection',
    data: {
      items: [
        {
          title: 'Item 1',
        },
        {
          title: 'Item 2',
        },
      ],
    },
    locale: 'en',
  })
  await payload.update({
    collection: 'my-collection',
    id: record.id,
    data: {
      items: [
        {
          title: 'Punkt 1',
        },
        {
          title: 'Punkt 2',
        },
      ],
    },
    locale: 'de',
  })

But the update seems to completely replace the original items - leaving the english language title empty.

obtuse vigilBOT
candid slate
#

Got the solution. You have to state the id with each array-item from the original record:

  await payload.update({
    collection: 'my-collection',
    id: record.id,
    data: {
      items: [
        {
          id: record.items![0].id,
          title: 'Punkt 1',
        },
        {
          id: record.items![1].id,
          title: 'Punkt 2',
        },
      ],
    },
    locale: 'de',
  })
sleek vessel
#

Nice, and thank you so much for coming back with the fix!