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.

Question about array field PUT rest api

default discord avatar
gonzamoiguer2 years ago
1 1

I have an array field with different data, to which payload adds an ID to each entry.
What I want to achieve is through a PUT request modify a single entry in this array, using this ID, but I can't find a way. Something like
content: {id: thirdItemID, value: newValue}

The only way for something like this to work is to send a request with the whole array and modifying the desired entry.

content: 
[
  {id: firstItemId, value: originalValue},
  {id: secondItemId, value: originalValue},
  {id: thirdItemID, value: newValue}, // <-- this is the one i'm editing
]

While writing this I thought about making a custom endpoint in my collection, but is there a simpler way around this? Thanks in advanced. Please let me know if i'm not being clear : )

  • Selected Answer
    discord user avatar
    DanRibbens
    2 years ago

    Hey @gonzam88,

    As you've figured already, sending the PUT with items missing from the array will update the "content" to be missing your other data and there isn't a way to tell Payload to leave them in. The array IDs was added to allow undefined fields to be saved correctly array objects shift around. There isn't a PATCH operation that would give you this today.

    You would need to make a custom endpoint for that. Alternatively you could structure your data to be an array that contains relationships. In that way the collection would have an endpoint that can modify one single item from having it's own collection, but that would have other implications to editor experience.

    Is there something in your implementation that makes it hard to fetch the full document and PUT it all back with the changes you need?
    Have you looked in to doing it with GraphQL? That might be an option but I need to dig more to find out.

    1 reply
  • default discord avatar
    gonzamoiguer2 years ago

    Thanks for the reply, just confirming my suspicions. I will PUT the whole array, just trying to check if there was a more straightforward way around this.

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.