Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Trouble with generating slug field in Sanity Studio

30 replies
Last updated: Feb 7, 2023
Hi Everyone,Excited to be here. I was wondering if anyone has come across the slug field not generating. Any tips to debug/trouble shoot?
Feb 2, 2023, 11:23 AM
If the generate is not working, make sure you have set the
options.source
value (link ) so it knows where to derive the slug from.
Feb 2, 2023, 11:44 AM
Thank you for responding User!It seems like I did set the value, here’s my schema:


import {defineField, defineType} from 'sanity'


export default defineType({

title: "Event", // title of the doc, display name for the Ui

name: "event", // when performing queries and hitting API's

type: "object",


fields: [

// always an array of objects

defineField({

title: "Event Name",

name: "eventName",

type: "string",

}),

defineField({

name: "slug",

title: "Slug",

type: "slug",

options: {

source: "eventName",

maxLength: 96,

},

}),

defineField({

name: 'ticketType',

title: 'Ticket Type',

type: 'string',

options: {

list: [

{ title: 'Ticketed', value: 'tix' },

{ title: 'Guest List', value: 'gl' },

{ title: 'Discounted', value: 'dic' },

{ title: 'Soldout', value: 'so' },

],

},

}),

defineField({

title: "Date",

name: "eventDate",

type: "date",

}),

defineField({

title: 'Event URL',

name: 'eventUrl',

type: 'url'

}),

defineField({

title: "Venue Name",

name: "venueName",

type: "string",

}),

defineField({

title: "Artists Performing",

name: "artists",

type: "string",

}),

defineField({

title: "Flyer",

name: "flyer",

type: "image",

options: {

hotspot: true,

}

}),

],


}

);
Feb 2, 2023, 8:49 PM
Any other approach?
Feb 2, 2023, 8:50 PM
Hmmm AFAIK this should work as long as you first enter an event name.
Feb 2, 2023, 8:54 PM
Yes I was also wondering, I tried everything 😭
Feb 2, 2023, 9:11 PM
The only thing I can think of is that defineField somehow messes with your schema?
Feb 2, 2023, 9:16 PM
Let me try it without it
Feb 2, 2023, 9:17 PM
Not working 🥲
Feb 2, 2023, 9:19 PM
If that doesn't work: I'd love to help but I'm going to bed soon.
user M
seems to be eager to help people here 😇
Feb 2, 2023, 9:19 PM
Thanks a lot User, sorry if I have kept you up late
Feb 2, 2023, 9:20 PM
Hopefully she can give you some insight, I'm by no means an expert.
Feb 2, 2023, 9:20 PM
Really value your time, much love
Feb 2, 2023, 9:20 PM
Haha no worries. I love to help.
Feb 2, 2023, 9:20 PM
Good luck!
Feb 2, 2023, 9:20 PM
Thanks again
Feb 2, 2023, 9:20 PM
What version of the Studio is this happening in?
Feb 2, 2023, 9:21 PM
Hi RD,How do I find that out?
Feb 2, 2023, 9:23 PM
I believe it’s v3
Feb 2, 2023, 9:23 PM
Your Sanity Studio is version
3.2.4
. The latest version is
3.2.6
.
Feb 2, 2023, 9:24 PM
It may be worth updating to see if the behavior still exists. If that doesn’t fix it I’ll replicate and see if I can fix it.
Feb 2, 2023, 9:24 PM
That is very kind of you
Feb 2, 2023, 9:24 PM
Let me go ahead and do that
Feb 2, 2023, 9:25 PM
I have updated, and the field is still not generating
Feb 2, 2023, 9:27 PM
I think something odd is happening since this slug field is inside of an object. It’s likely looking for an
eventName
on the document and not finding that field since it’s nested under your
event
. I was able to get it to work by changing the source to the following:
source: (doc, { parent }) => parent.eventName,
Feb 2, 2023, 9:39 PM
I see what is happening, since it’s an array of events. This actually makes total sense, oh my goodness I can’t thank you enough
Feb 2, 2023, 9:44 PM
Happy to help!
Feb 2, 2023, 9:57 PM
Hello
user M
Sorry to bother you again, but I am having a hard time figuring out the right GROQ query to filter by the slugs that sit into the event category in the events document.

The [Object] below represent an event document/category


[

{

_createdAt: '2023-01-27T10:50:55Z',

_id: '26d56839-2903-4353-b039-1365f2100f81',

_rev: '0mmgc4CDWeXHLRG7XO0GKF',

_type: 'events',

_updatedAt: '2023-02-04T05:18:56Z',

city: 'sc',

events: [ [Object] ]

},

{

_createdAt: '2023-01-27T10:58:46Z',

_id: 'f3ec5bbf-e124-4503-9b58-917dee7e6569',

_rev: '0mmgc4CDWeXHLRG7X7zkHP',

_type: 'events',

_updatedAt: '2023-02-03T03:35:29Z',

city: 'sf',

events: [ [Object], [Object] ]

}

]
Feb 4, 2023, 5:49 AM
UPDATE: I believe this worked, however how do I compare and filter it with the local $slug
*[_type=='events']  {

events[] {

slug

}

}

I have attempted, but unfortunately it did not work

 *[_type=='event' && event.slug.current == "perro-paradise"]
Thanks again
Feb 4, 2023, 5:57 AM
What query are you currently using?
Feb 7, 2023, 7:37 PM
HI RD!!I’m extremely happy to hear from you, I’m using GROQ and have actually solved this on the frontend. However, I have pulled an all nighter attempting to push my studio to dev with no luck. I have just sent you a message!
Feb 7, 2023, 10:52 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?