Discussion about querying categories in Sanity.io blog posts schema

58 replies
Last updated: Dec 29, 2022
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
Have you tried removing the [0]? You shouldn't have multiple slugs with the same name.
Dec 29, 2022, 4:38 PM
thats unrelated
Dec 29, 2022, 4:39 PM
i'm returning object inside
Dec 29, 2022, 4:39 PM
Oh my bad
Dec 29, 2022, 4:49 PM
Sorry i don't know how to return the specific object without like above syntax
Dec 29, 2022, 4:49 PM
I just want to get my categories attached to my blog post.
Dec 29, 2022, 4:50 PM
This is my categories in blog

{
      name: 'categories',
      title: 'Categories',
      type: 'array',
      of: [{ type: 'reference', to: { type: 'category' } }],
    },
Dec 29, 2022, 4:50 PM
Can you help ?
Dec 29, 2022, 4:50 PM
I have a similar call but I used a function and passed in the
$slug
as a parameter of the function and it works.
Dec 29, 2022, 4:51 PM
to return the categories document you would need to instead of
categories
it would be
categories[]->
Dec 29, 2022, 4:53 PM
that doesn't work as well
Dec 29, 2022, 4:53 PM
tried it
Dec 29, 2022, 4:53 PM
i can see my categories in my blog
Dec 29, 2022, 4:55 PM
so they do exists
Dec 29, 2022, 4:55 PM
is it returning
null
?
Dec 29, 2022, 4:58 PM
Did you try the query in vision? Do you have a reference to categories in your posts schema?
Dec 29, 2022, 4:58 PM
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
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
i do have
Dec 29, 2022, 5:00 PM
categories field is never returned in post
Dec 29, 2022, 5:01 PM
it's just empty array or null depending on what i do
Dec 29, 2022, 5:01 PM
  {
    _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
It would make it much easier using vision. You can see where the issue is coming from
Dec 29, 2022, 5:01 PM
what's a vision ?
Dec 29, 2022, 5:01 PM
in frontend
Dec 29, 2022, 5:01 PM
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
With vision you can query your data using GROQ.
Dec 29, 2022, 5:03 PM
I use Vision so I could backtrack where the issue is coming from
Dec 29, 2022, 5:05 PM
You also have
to: {type: category}
it should be
categories
as that is the name given for the other schema.
Dec 29, 2022, 5:08 PM
Either that or your query should just be
category.
Dec 29, 2022, 5:09 PM
He actually has the type correct
user G
Dec 29, 2022, 5:11 PM
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
Nevermind I see
Dec 29, 2022, 5:15 PM
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
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
It could be breaking the reference. Maybe looked for a groq plug-in for highlighting.
Dec 29, 2022, 5:33 PM
user G
what you mean? Should it be just
to: category
instead of
to: { type: 'category' }
Dec 29, 2022, 5:37 PM
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
There should not be a second curly brace after category
Dec 29, 2022, 5:41 PM
When you create the post does it give a list of created categories to choose from?
Dec 29, 2022, 5:45 PM
try
categories->
Dec 29, 2022, 5:50 PM
I just realized that now it should be
of: [{ type: 'reference', to: [{ type: 'category' }] }]
Dec 29, 2022, 5:59 PM
you forgot to add the square brackets first to
to
Dec 29, 2022, 6:00 PM
You have it set as
to: { type: 'category' }
when it should be
to: [{ type: 'category' }]
Dec 29, 2022, 6:03 PM
That's at least how I have my documents set up
Dec 29, 2022, 6:03 PM
Its a square bracket because you could reference multiple types
Dec 29, 2022, 6:05 PM
The Reference document shows to as an array instead of an object
Dec 29, 2022, 6:06 PM
Is this solved?
Dec 29, 2022, 6:11 PM
no
Dec 29, 2022, 6:14 PM
new post returns categories
Dec 29, 2022, 6:14 PM
but not the old post
Dec 29, 2022, 6:14 PM
the old post doesnt return categories?
Dec 29, 2022, 6:15 PM
Have you made sure the old post has added a category and published it?
Dec 29, 2022, 6:16 PM
user C
brackets solved
Dec 29, 2022, 6:18 PM
could it be in past versions sanity was using non bracket
Dec 29, 2022, 6:19 PM
you should be able to use
categories[]->
afterwords
Dec 29, 2022, 6:19 PM
i installed it 1 year ago
Dec 29, 2022, 6:19 PM
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.

Was this answer helpful?