Sanity transaction.patch().set() is not a function error when updating posts
The issue you're encountering is with the method chaining syntax for the Sanity client's transaction API. The patch() method doesn't support chaining .set() in transactions the way you're trying to use it.
Here's the corrected syntax for your function:
const fetchInstagramPosts = async () => {
const existingPosts = await client.fetch(
`*[_type == "instagramPost"] {
_id,
instagramId
}`
);
try {
const url = `https://graph.instagram.com/me/media?fields=id,media_url,timestamp&access_token=${INSTAGRAM_KEY}`;
const res = await fetch(url);
const data = await res.json();
if (!data || !data.data) {
console.error("Error: Instagram API returned an unexpected response", data);
return;
}
const transaction = client.transaction();
for (const post of data.data) {
const existingPost = existingPosts.find(
(p: any) => p.instagramId === post.id
);
if (existingPost) {
// Correct syntax: pass the patch spec as the second argument
transaction.patch(existingPost._id, {
set: {
mediaUrl: post.media_url,
timestamp: post.timestamp,
}
});
} else {
const newPost = {
_type: "instagramPost",
instagramId: post.id,
mediaUrl: post.media_url,
timestamp: post.timestamp,
articleLink: "https://www.nordkurier.de/",
};
transaction.create(newPost);
}
}
await transaction.commit();
} catch (error) {
console.error("Error fetching Instagram posts", error);
}
};The key change is in how you call patch(). Instead of:
transaction.patch(existingPost._id).set({ ... })Use:
transaction.patch(existingPost._id, { set: { ... } })The patch operation in Sanity transactions takes the document ID as the first argument and a patch specification object as the second argument. The patch spec object should contain the operation type (set, unset, setIfMissing, etc.) as a key with the fields to modify as its value.
This should resolve your "transaction.patch(...).set is not a function" error and allow you to update existing Instagram posts while creating new ones in a single transaction.
Show original thread1 reply
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.