Discussion about updating child properties using the JS client
26 replies
Last updated: Jul 10, 2023
K
I'm using js client and trying to update a child property below but don't know how to
I'm able to delete an item the same way.
const response = await client .patch(boardId) .set([`list[_key=="${laneId}"].name : ${title}`]) .commit();
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
K
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
K
Uncaught (in promise) Error: set() takes an object of properties at exports.validateObject (validators.js
:28:1) at Patch._assign (patch.js
:118:1)
: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
K
.set([`list[_key=="${laneId}"].name : ${title}`])
Feb 10, 2023, 6:26 PM
K
is that doable?
Feb 10, 2023, 6:26 PM
K
I tried something like set(['list[key==$$].name , VALUE ])
Feb 10, 2023, 6:26 PM
K
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
K
maybe some similar samples on how to use the client? or any documentation with samples?
Feb 10, 2023, 6:31 PM
K
and what is that string?
Feb 10, 2023, 6:33 PM
K
conditions? I already read those
Feb 10, 2023, 6:33 PM
K
there is no sample with set({ fieldKey, fieldObjectWithChildren })
what I am trying to do is exactly
after the operation set(['list[key=1234].name: Y)
I want this
what I am trying to do is exactly
myObject { list [ { _key: 1234 name: x }, {}, {} ]}
I want this
myObject { list [ { _key: 1234 name: Y }, {}, {} ]}
Feb 10, 2023, 6:36 PM
K
nameproperty of the first item with key=1234 of the
listproperty 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
K
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
K
I am only looking for a way to set({key.key.key : value }) kind of process
Feb 10, 2023, 6:47 PM
K
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
K
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
K
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
K
Is there any sample and I am missing it?
Feb 10, 2023, 6:54 PM
K
sorry my bad. i didnt see that 'replace' option in inserrt
Feb 12, 2023, 3:38 PM
K
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
W
I’m very lucky to have found this Slack conversation with the final solution posted by
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.
My silently failing code was
The working code is
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()
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.