#How to update a specific index of an array field?

1 messages · Page 1 of 1 (latest)

winter moat
#

I'm just getting started with Payload and I've run into a wall trying to figure out how to update a specific index of an array field.

Consider that I have a collection named animals and it has an array field called cats.

import { CollectionConfig } from 'payload/types'

const Animals: CollectionConfig = {
  slug: 'animals',
  ...
  fields: [
    {
      name: 'cats',
      type: 'array',
      fields: [
        {
          name: 'name',
          type: 'text'
        }
      ]
    }
  ]

If I fire a GET https://myapi.com/api/cats request I get the following:

{
  docs: [
    {
      id: 'abcdefg12345',
      ...
      cats: [
        {
           id: 'abcdefg12346',
           name: 'Ava' 
        },
        {
           id: 'abcdefg12347',
           name: 'Guli' 
        }
      ]
    } 
  ]
}

How would I go about updating the second item in the cats array (id: 'abcdefg12347) to correct the name from "Guli" to "Gulu"?

I've scoured the docs but I haven't discovered any explanation or examples regarding this type of op.

rigid riverBOT