✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

How to use the Patch Set Function to Set a Slug

4 replies
Last updated: May 30, 2022
Is there a way to use the
patch
set
function to set a slug? I've made a few attempts, but each time I get complaints about it being a
string
or
number
.
May 30, 2022, 3:53 PM
You can. A
slug
is an object with a
_type
(always
'slug'
) and a
current
value (that’s what you want the slug to be), so you’ll need to make sure to patch the entire object.
May 30, 2022, 4:01 PM
So would that be something like this?

patch.execute([
  {
    set: {
      slug: { _type: "slug", current: query },
    },
  },
]);
Or is there a better way?
😅
May 30, 2022, 4:05 PM
I’m not familiar with patch.execute, but I tested the following and it worked:

client
  .patch(docId)
  .set({slug: {_type: "slug", current: query}})
  .commit()
  .then((updatedDoc) => {
    console.log('Update successful! New document:')
    console.log(updatedDoc)
  })
  .catch((err) => {
    console.error('Update failed: ', err.message)
  })
May 30, 2022, 4:13 PM
I'll give that option a look, thank you 🙂
May 30, 2022, 4:15 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?