Discussion about querying categories in Sanity.io blog posts schema
58 replies
Last updated: Dec 29, 2022
M
can someone help ?
const query2 = `*[_type == "post" && slug.current == $slug] [0] { _id, _createdAt, title, author-> { name, image }, description, mainImage, slug, body, readTime, categories }`;
Dec 29, 2022, 4:36 PM
K
Have you tried removing the [0]? You shouldn't have multiple slugs with the same name.
Dec 29, 2022, 4:38 PM
M
thats unrelated
Dec 29, 2022, 4:39 PM
M
i'm returning object inside
Dec 29, 2022, 4:39 PM
K
Oh my bad
Dec 29, 2022, 4:49 PM
M
Sorry i don't know how to return the specific object without like above syntax
Dec 29, 2022, 4:49 PM
M
I just want to get my categories attached to my blog post.
Dec 29, 2022, 4:50 PM
M
This is my categories in blog
{ name: 'categories', title: 'Categories', type: 'array', of: [{ type: 'reference', to: { type: 'category' } }], },
Dec 29, 2022, 4:50 PM
M
Can you help ?
Dec 29, 2022, 4:50 PM
K
I have a similar call but I used a function and passed in the
$slugas a parameter of the function and it works.
Dec 29, 2022, 4:51 PM
S
to return the categories document you would need to instead of
categoriesit would be
categories[]->
Dec 29, 2022, 4:53 PM
M
that doesn't work as well
Dec 29, 2022, 4:53 PM
M
tried it
Dec 29, 2022, 4:53 PM
M
i can see my categories in my blog
Dec 29, 2022, 4:55 PM
M
so they do exists
Dec 29, 2022, 4:55 PM
S
is it returning
null?
Dec 29, 2022, 4:58 PM
K
Did you try the query in vision? Do you have a reference to categories in your posts schema?
Dec 29, 2022, 4:58 PM
M
export default createSchema({ // We name our schema name: 'default', // Then proceed to concatenate our document type // to the ones provided by any plugins that are installed types: schemaTypes.concat([ // The following are document types which will appear // in the studio. post, author, category, // When added to this list, object types can be used as // { type: 'typename' } in other document schemas blockContent, ]), })
Dec 29, 2022, 5:00 PM
M
export default { name: 'post', title: 'Post', type: 'document', fields: [ { name: 'title', title: 'Title', description: 'Keep titles short!', type: 'string', }, { name: 'description', title: 'Description', description: 'Keep descriptions short!', type: 'string', }, { name: 'slug', title: 'Slug', type: 'slug', options: { source: 'title', maxLength: 96, }, }, { name: 'author', title: 'Author', type: 'reference', to: { type: 'author' }, }, { name: 'mainImage', title: 'Main image', type: 'image', options: { hotspot: true, }, }, { name: 'categories', title: 'Categories', type: 'array', of: [{ type: 'reference', to: { type: 'category' } }], }, { name: 'readTime', title: 'Read time', type: 'string', }, { name: 'publishedAt', title: 'Published at', type: 'datetime', }, { name: 'body', title: 'Body', type: 'blockContent', }, ],
Dec 29, 2022, 5:00 PM
M
i do have
Dec 29, 2022, 5:00 PM
M
categories field is never returned in post
Dec 29, 2022, 5:01 PM
M
it's just empty array or null depending on what i do
Dec 29, 2022, 5:01 PM
M
{ _createdAt: '2022-10-07T10:00:30Z', _id: 'a17afd0d-bf0f-4311-8b84-d9b70c0f665d', _rev: 't1zFG7IFB4LMSgJOyrY0ya', _type: 'post', _updatedAt: '2022-12-20T15:06:53Z', author: { _ref: 'ac8b05c9-e48c-4e6c-a601-a743bb2c98b3', _type: 'reference' }, body: [ [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object] ], description: 'Mobiele apps die niet succesvol zijn, hebben één ding gemeen: een slechte gebruikerservaring. Naarmate mobiele platforms zich blijven ontwikkelen en ontwikkelen, wordt het steeds belangrijker dat de gebruikerservaring top of mind blijft.', mainImage: { _type: 'image', asset: [Object] }, publishedAt: '2022-12-20T15:06:00.000Z', slug: { _type: 'slug', current: '10-best-practices-om-de-gebruikerservaring-van-je-mobiele-app-te-verbeteren' }, title: '10 beste manieren om de gebruikerservaring van je mobiele app te verbeteren' },
Dec 29, 2022, 5:01 PM
S
It would make it much easier using vision. You can see where the issue is coming from
Dec 29, 2022, 5:01 PM
M
what's a vision ?
Dec 29, 2022, 5:01 PM
M
in frontend
Dec 29, 2022, 5:01 PM
S
if you get on sanity studio, you should see two tabs where it says Desk and Vision on the very top of the page
Dec 29, 2022, 5:02 PM
S
With vision you can query your data using GROQ.
Dec 29, 2022, 5:03 PM
S
I use Vision so I could backtrack where the issue is coming from
Dec 29, 2022, 5:05 PM
K
You also have
to: {type: category}it should be
categoriesas that is the name given for the other schema.
Dec 29, 2022, 5:08 PM
K
Either that or your query should just be
category.
Dec 29, 2022, 5:09 PM
S
He actually has the type correct
user G
Dec 29, 2022, 5:11 PM
K
The reference in the post schema is to type category but in the query it is asking for categories
Dec 29, 2022, 5:14 PM
K
Nevermind I see
Dec 29, 2022, 5:15 PM
S
I'm wondering if the first document he is referencing does not have a category selected which is why its an empty array or null
Dec 29, 2022, 5:17 PM
K
Ah this could be nothing but seems like a syntax error. In the schema there is
to: {type : 'category' } }]There is no need for the second curly brace.
Dec 29, 2022, 5:33 PM
K
It could be breaking the reference. Maybe looked for a groq plug-in for highlighting.
Dec 29, 2022, 5:33 PM
M
user G
what you mean? Should it be just to: categoryinstead of
to: { type: 'category' }
Dec 29, 2022, 5:37 PM
K
No I believe that is correct as long as the name in the category schema is the same. Check the syntax something look funny there.
Dec 29, 2022, 5:38 PM
K
There should not be a second curly brace after category
Dec 29, 2022, 5:41 PM
K
When you create the post does it give a list of created categories to choose from?
Dec 29, 2022, 5:45 PM
B
try
categories->
Dec 29, 2022, 5:50 PM
S
I just realized that now it should be
of: [{ type: 'reference', to: [{ type: 'category' }] }]
Dec 29, 2022, 5:59 PM
S
you forgot to add the square brackets first to
to
Dec 29, 2022, 6:00 PM
S
You have it set as
to: { type: 'category' }when it should be
to: [{ type: 'category' }]
Dec 29, 2022, 6:03 PM
S
That's at least how I have my documents set up
Dec 29, 2022, 6:03 PM
S
Its a square bracket because you could reference multiple types
Dec 29, 2022, 6:05 PM
S
The Reference document shows to as an array instead of an object
Dec 29, 2022, 6:06 PM
H
Is this solved?
Dec 29, 2022, 6:11 PM
M
no
Dec 29, 2022, 6:14 PM
M
new post returns categories
Dec 29, 2022, 6:14 PM
M
but not the old post
Dec 29, 2022, 6:14 PM
H
the old post doesnt return categories?
Dec 29, 2022, 6:15 PM
H
Have you made sure the old post has added a category and published it?
Dec 29, 2022, 6:16 PM
M
user C
brackets solvedDec 29, 2022, 6:18 PM
M
could it be in past versions sanity was using non bracket
Dec 29, 2022, 6:19 PM
S
you should be able to use
categories[]->afterwords
Dec 29, 2022, 6:19 PM
M
i installed it 1 year ago
Dec 29, 2022, 6:19 PM
S
It could possibly be that they didn't support multiple references but I just started using sanity 1 month ago, so I'm not too sure
Dec 29, 2022, 6:20 PM
Sanity– build remarkable experiences at scale
The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.