👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Issue with fetching Mux video type in Sanity schema using Groq

19 replies
Last updated: Feb 7, 2022
hi guys I have some issues working with the mux video type
Feb 7, 2022, 4:59 PM
if added it to my schema's like this
Feb 7, 2022, 4:59 PM
{
  title: 'Content',
  name: 'content',
  type: 'array',
  of: [
    {
      type: 'block'
    }, {
      type: "mux.video"
    },
    {
      type: 'image',
      options: {
        hotspot: true
      },
      fields: [
        {
          name: 'caption',
          type: 'string',
          title: 'Caption',
          options: {
            isHighlighted: true // <-- make this field easily accessible
          }
        },
      ]

    }
  ]
},

Feb 7, 2022, 5:00 PM
but when I fetch it with groq like this
Feb 7, 2022, 5:00 PM
"lesson": *[slug.current == $lessonSlug][0] { 
    _id,
    slug,
    title,
    content,
}

Feb 7, 2022, 5:00 PM
its not actually fetching the mux video
Feb 7, 2022, 5:00 PM
it returns
Feb 7, 2022, 5:01 PM
1.
*asset*:
a.
*_ref*: "1eaec214-7eac-4b97-95fa-b843fa4373b2"
b.
[[Prototype]]: Object
2.
*_key*: "91498c943dd0"
3.
*_type*: "mux.video"

Feb 7, 2022, 5:01 PM
like this
Feb 7, 2022, 5:01 PM
with no actual reference to the mux video ID I need
Feb 7, 2022, 5:01 PM
I know you are can fetch references with the arrow in groq but I don't know how to do it with the array type. for images it seems to do it on its own
Feb 7, 2022, 5:02 PM
content[]-&gt;{}
Feb 7, 2022, 5:11 PM
hmm
Feb 7, 2022, 5:14 PM
I got it working by using
content[] {
    ...,
    _type == "mux.video" => {
        asset->,
    },
}
Feb 7, 2022, 5:14 PM
yep, I am curious why you chose an array over document for the schema?
Feb 7, 2022, 5:17 PM
Glad you got it working.
Feb 7, 2022, 5:18 PM
its the content of a page
Feb 7, 2022, 8:02 PM
you would have made a document out of it and referenced it?
Feb 7, 2022, 8:02 PM
const lesson = {
  name: 'lesson',
  title: 'Lesson',
  type: 'document',
  fields: [
    {
      title: 'Titel',
      name: 'title',
      type: 'string',
    },
    {
      title: 'Slug',
      name: 'slug',
      type: 'slug',
      options: {
        source: 'title',
        maxLength: 200, // will be ignored if slugify is set
        slugify: input => input
            .toLowerCase()
            .replace(/\s+/g, '-')
            .slice(0, 200)
      }
    },
    {
      title: 'Content',
      name: 'content',
      type: 'array',
      of: [
        {
          type: 'block'
        }, {
          type: "mux.video"
        },
        {
          type: 'image',
          options: {
            hotspot: true
          },
          fields: [
            {
              name: 'caption',
              type: 'string',
              title: 'Caption',
              options: {
                isHighlighted: true // <-- make this field easily accessible
              }
            },
          ]

        }
      ]
    },
  ]
}

export default lesson;

Feb 7, 2022, 8:02 PM
this is my entire schema
Feb 7, 2022, 8:02 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?