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

Issue with fetching null values from schema and query in Slack thread

6 replies
Last updated: Sep 19, 2022
help Some values of schema is coming null while trying to fetch them
This is my schema of the cases


export default {
    name: 'answerQuestion',
    title: 'Answer Question',
    type: 'document',
    fields: [
      {
        name: 'title',
        title: 'Title',
        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: 'publishedAt',
        title: 'Published at',
        type: 'datetime',
      },
      {
        name: 'body',
        title: 'Body',
        type: 'blockContent',
      },
      {
        name:"askedby",
        title:"Asked By",
        type:"string"
      },
      {
        name:"videoURL",
        title:"videoURL",
        type:"string",
      },{
        name:"refrences",
        title:"refrences",
        type:"string",
      }
    ],
  
    preview: {
      select: {
        title: 'title',
        author: 'author.name',
        media: 'mainImage',
      },
      prepare(selection) {
        const {author} = selection
        return Object.assign({}, selection, {
          subtitle: author && `by ${author}`,
        })
      },
    },
  }
  
I am facing issue with date and refrence HERE IS MY QUERY


const data = await client.fetch(`*[_type == "answerQuestion" && slug.current == "${context.query.slug}"]{
        title,
        slug,
        mainImage{
            asset->{
                url
            }   
        },
        categories[]->{
            title
        },
        author->{
            name,
           
        },
        body,
        refrence,
        _createdAt,
        _updatedAt

    }`);
Sep 19, 2022, 7:50 AM
What values are coming null?
Sep 19, 2022, 8:12 AM
Have you considered that the data may be missing which is why you get null?
Sep 19, 2022, 8:12 AM
Unrelated note: You shouldn’t inject parameters like this and instead rely on the parameter object from the fetch method. See: https://www.sanity.io/docs/groq-parameters
Sep 19, 2022, 8:13 AM
Comparing your query with your schema, you query
refrence
but your schema has a field called
refrences
(plural). You may also want to rename that field to avoid the typo (ref**e**rence). 🙂
Sep 19, 2022, 8:14 AM
now i changed my schema

export default {
    name: 'cases',
    title: 'cases',
    type: 'document',
    fields: [
      {
        name: 'title',
        title: 'Title',
        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: 'date',
        title: 'Date',
        type: 'date',
      },
      {
        name: 'body',
        title: 'Body',
        type: 'blockContent',
      },
      {
        name:"description",
        title:"Description",
        type:"text",
      },{
        name:"country",
        title:"Country",
        type:"string",
      },{
        name:"state",
        title:"State",
        type:"string",
      },{
        name:"district",
        title:"District",
        type:"string",
      },{
        name:"proofOFCase",
        title:"Proof Of Case",
        type:"text",
      }
    ],
  
    preview: {
      select: {
        title: 'title',
        author: 'author.name',
        media: 'mainImage',
      },
      prepare(selection) {
        const {author} = selection
        return Object.assign({}, selection, {
          subtitle: author && `by ${author}`,
        })
      },
    },
  }
  
AND CHANGE MY QUERY TO


const data = await client.fetch(`*[_type == "answerQuestion" && slug.current == "${context.query.slug}"]{
        title,
        slug,
        mainImage{
            asset->{
                url
            }   
        },
        categories[]->{
            title
        },
        author->{
            name,
           
        },
        body,
        proofOFCase,
        _createdAt,
        _updatedAt,
        date,

    }
AND THE RESPONSE NULL
Sep 19, 2022, 8:33 AM
user F
i am so sorry to bother you i have fixed the issue thanks for your precious time
Sep 19, 2022, 8:49 AM

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?