'How to delete a document attribute in Sanity iO?
I have an array of objects in my Sanity Document called Images called Comments
an example comment object in comments[] array looks like :
{
"_key": "6510dc79cf8b",
"comment": "Hello world",
"postedBy": {
"_id": "117108441389496202965",
"image": "https://lh3.googleusercontent.com/a-/AOh14Ggq3iKH-nketDY9Qx7Y2Yva09E5_2WNJYVr77AA9AQ=s96-c",
"userName": "haha"
}
}
I want to delete this comment in the comments array which exists in the Images document.
In my Image schema Sanity looks like :
{
name: "comments",
title: "Comments",
type: "array",
of: [{ type: "comment" }],
},
],
};
I'm trying to delete this comment by
const deleteComment = async (key) => {
try {
client
.patch(pinId)
.delete([{ comment, _key: key, postedBy: { _type: "postedBy", _ref: user._id } }])
.commit()
.then(() => {
fetchPinDetails();
setComment("");
setAddingComment(false);
});
window.location.reload();
} catch (error) {
console.log(error);
}
};
But delete() doesnt exist on patch.
IS there another way to do this simple query (if it was in SQL)? Driving me nuts
Solution 1:[1]
You should be able to do it using unset. Something like this:
client.patch(pinId).unset([`comments[_key==${key}]`]).commit()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | corygibbons |
