Trouble with generating slug field in Sanity Studio
30 replies
Last updated: Feb 7, 2023
T
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?
A
If the generate is not working, make sure you have set the
options.sourcevalue (link ) so it knows where to derive the slug from.
T
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,
}
}),
],
}
);
T
Any other approach?
A
Hmmm AFAIK this should work as long as you first enter an event name.
T
Yes I was also wondering, I tried everything 😭
A
The only thing I can think of is that defineField somehow messes with your schema?
T
Let me try it without it
T
Not working 🥲
A
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 😇T
Thanks a lot User, sorry if I have kept you up late
A
Hopefully she can give you some insight, I'm by no means an expert.
T
Really value your time, much love
A
Haha no worries. I love to help.
A
Good luck!
T
Thanks again
T
Hi RD,How do I find that out?
T
I believe it’s v3
T
Your Sanity Studio is version
3.2.4. The latest version is
3.2.6.
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.
T
That is very kind of you
T
Let me go ahead and do that
T
I have updated, and the field is still not generating
I think something odd is happening since this slug field is inside of an object. It’s likely looking for an
eventNameon 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,T
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
T
Hello
The [Object] below represent an event document/category
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] ]
}
]
T
UPDATE: I believe this worked, however how do I compare and filter it with the local $slug
I have attempted, but unfortunately it did not work
Thanks again
*[_type=='events'] { events[] { slug
}
}
I have attempted, but unfortunately it did not work
*[_type=='event' && event.slug.current == "perro-paradise"]
T
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!
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.