Issue with updating a boolean value in a mutation resulting in a 403 error.
11 replies
Last updated: Feb 1, 2023
R
Hi All, I'm trying to a mutation to change a boolean true to false and getting this 403 error in the network tab
My write token is editor so I'm not what else to try. Any help would be appreciated
{ "error": { "description": "Mutation(s) failed with 1 error(s)", "items": [ { "error": { "description": "Insufficient permissions; permission \"update\" required", "permission": "update", "type": "insufficientPermissionsError" }, "index": 0 } ], "type": "mutationError" } }
Jan 26, 2023, 4:02 AM
R
This is the function I'm hoping makes the mutation
export async function decQuanity(id: string, token?: string | null): Promise<void> { if (projectId) { const client = createClient({ projectId, dataset, apiVersion, useCdn, token: token || undefined }) console.log(token) await client.patch(id).set({ forSale: false }).commit() } }
Jan 26, 2023, 4:07 AM
R
I'm using it in an onclick event. It's store where a painting is either for sale or not for defined by the boolean forSale. I was hoping that when someone adds the painting to the cart, I could could mutate the boolean to false so some else could not buy the paining.
Jan 26, 2023, 6:03 AM
Thank you. I’m not actually sure that
createClientof
next-sanitycan take a write token (but I’m happy to be proven wrong). I’ll have to take a closer look tomorrow (unless you get an answer before that).
Jan 26, 2023, 6:24 AM
R
Thanks, have a good night
Jan 26, 2023, 6:27 AM
R
Good Morning, I tried this but still 403
I have a token on the client = to
export async function decQuanity(id: string): Promise<void> { const mutations = [ { patch: { id, set: { forSale: false } } } ] if (client) { await client.patch(id).set({ mutations }).commit() } }
export const token = process.env.SANITY_API_WRITE_TOKEN
Jan 26, 2023, 4:10 PM
R
Ahhhh needed Next_Public_ on the env. Now just to get the patch event lined up with the schema
Jan 26, 2023, 4:16 PM
S
Hello User, easy peasy:
client.patch(id).set({ forSale: false }).commit()
Feb 1, 2023, 11:37 AM
S
You are mixing the http API for mutations with the client and thus setting your mutations array as an array called mutations with you patch object as an item. 😉
here is all you need
👉 https://www.sanity.io/docs/js-client#patch-update-a-document
here is all you need
👉 https://www.sanity.io/docs/js-client#patch-update-a-document
Feb 1, 2023, 11:39 AM
R
Thank you
user J
, start easy peasy, then complicate your coding life, I like that. I had figured the set but hadn't thought to read the sanity client documentation so that is 🥇.Feb 1, 2023, 4:02 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.