Unlock seamless workflows and faster delivery with our latest releases – get the details

Discussion about updating child properties using the JS client

26 replies
Last updated: Jul 10, 2023
I'm using js client and trying to update a child property below but don't know how to
const response = await client
    .patch(boardId)
    .set([`list[_key=="${laneId}"].name : ${title}`])
    .commit();
I'm able to delete an item the same way.

const response = await client
    .patch(boardId)
    .unset([`list[_key=="${laneId}"]`])
    .commit();
Feb 8, 2023, 12:19 AM
What issues are you having? Is it not updating anything or not updating the correct thing?
Feb 8, 2023, 4:54 PM
nothing. Just error. I am expecting the first snippet to update a child item. Any chance you can give me a sample code writting with js client?
Feb 9, 2023, 8:02 PM
Can you share the error?
Feb 9, 2023, 8:05 PM
Uncaught (in promise) Error: set() takes an object of properties at exports.validateObject (validators.js
:28:1) at Patch._assign (patch.js
:118:1)
Feb 10, 2023, 1:48 AM
Ah, as the error mentions, set takes an object with key value pairs. you’re just passing in an array.
Feb 10, 2023, 6:25 PM
.set([`list[_key=="${laneId}"].name : ${title}`])
you mean in this line? I need to change the child element and it needs to match key first
Feb 10, 2023, 6:26 PM
is that doable?
Feb 10, 2023, 6:26 PM
I tried something like set(['list[key==$$].name , VALUE ])
Feb 10, 2023, 6:26 PM
I honestly dont understand the logic here. What is the argument inside the set function anyways? An array of setters?
Feb 10, 2023, 6:27 PM
maybe some similar samples on how to use the client? or any documentation with samples?
Feb 10, 2023, 6:31 PM
You’re currently passing in an array with a string inside of it. If you’re having trouble understanding, this and this should help you.
Feb 10, 2023, 6:32 PM
and what is that string?
Feb 10, 2023, 6:33 PM
conditions? I already read those
Feb 10, 2023, 6:33 PM
there is no sample with set({ fieldKey, fieldObjectWithChildren })
what I am trying to do is exactly

myObject {
 list [
{
 _key: 1234
name: x },
{},
{}
]}
after the operation set(['list[key=1234].name: Y)
I want this

myObject {
 list [
{
 _key: 1234
name: Y },
{},
{}
]}
Feb 10, 2023, 6:36 PM
name
property of the first item with key=1234 of the
list
property in the object
Feb 10, 2023, 6:38 PM
I’d suggest carefully reading your code and comparing it to the documentation I linked. As I mentioned here , you are not submitting and object.
Feb 10, 2023, 6:44 PM
So, what I am trying to do is not possible? I can use client.patch.set({ key: value}) and this works.
Feb 10, 2023, 6:46 PM
I am only looking for a way to set({key.key.key : value }) kind of process
Feb 10, 2023, 6:47 PM
so, what I am looking for is not in the documentation
Feb 10, 2023, 6:48 PM
No, it is possible and in the documentation. However, the code you’ve written is not doing what you think it’s doing. I unfortunately can’t provide any more help if you’re not going to look critically at your code.
Feb 10, 2023, 6:51 PM
I am not going to look critically at my code? It's just one line of code and I'm asking a functionality or a sample code reference. Not to do my code
Feb 10, 2023, 6:53 PM
And all of the patch samples in the links are only setting one field of the document. Not children elements.
Feb 10, 2023, 6:53 PM
Is there any sample and I am missing it?
Feb 10, 2023, 6:54 PM
sorry my bad. i didnt see that 'replace' option in inserrt
Feb 12, 2023, 3:38 PM
for those who are looking for a solution for a similar problem, the following is the solution

  const response = await client
    .patch(boardId)
    .insert('replace', `list[_key=="${laneId}"]`, [{
      ...lane,
      ['name']: title
    }])
    .commit();
Feb 12, 2023, 3:39 PM
I’m very lucky to have found this Slack conversation with the final solution posted by
user G
.
I spent many hours looking for how to do this in the documentation and wasn’t able to find what I was looking for. I also wasn’t getting an error message at all. All I could see was that the patch wasn’t working.


user M
I’d like to provide some feedback on this -- the documentation on this part of the API needs more attention 🙏 for future info seekers.
My silently failing code was


const response = await sanityClient
      .patch(workEntry.profileId)
      .set({ `workEntries[_key=="${workEntry._key}"].specificCompany`: false })
      .commit()
The working code is

const response = await sanityClient
      .patch(workEntry.profileId)
      .insert('replace', `workEntries[_key=="${workEntry._key}"]`, [
        {
          ...profile,
          ['specificCompany']: false
        }
      ])
      .commit();
Jul 10, 2023, 7:23 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?