👀 See Sanity in action: Watch product demo 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.io – build remarkable experiences at scale

Sanity is a customizable solution that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Categorized in