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

    }`);
AI Update

I can see the issue in your code! There are a couple of problems causing null values:

1. Typo in field name: refrence vs refrences

In your schema, you defined the field as refrences (with an 'e'), but in your GROQ query you're requesting refrence (without the 'e'). This mismatch means you're querying for a field that doesn't exist, so it returns null.

2. Missing publishedAt in your query

You have a publishedAt field in your schema (which is your date field), but you're not requesting it in your GROQ query. You're only fetching _createdAt and _updatedAt.

Here's your corrected query:

const data = await client.fetch(`*[_type == "answerQuestion" && slug.current == "${context.query.slug}"]{
    title,
    slug,
    mainImage{
        asset->{
            url
        }   
    },
    categories[]->{
        title
    },
    author->{
        name
    },
    body,
    refrences,  // Fixed spelling to match schema
    publishedAt, // Added the date field from your schema
    askedby,    // You might want this too
    videoURL,   // And this
    _createdAt,
    _updatedAt
}`);

Additional recommendations:

  1. Use parameterized queries to prevent GROQ injection vulnerabilities:
const data = await client.fetch(
  `*[_type == "answerQuestion" && slug.current == $slug]{
    title,
    slug,
    // ... rest of fields
  }`,
  { slug: context.query.slug }
);
  1. Check your Studio - make sure you've actually published the documents with values in those fields. If you're fetching with the default published perspective (as of API version 2025-02-19), only published documents with published field values will appear.

  2. Verify the author reference exists - if the author field is null, make sure you've actually selected an author document in the Studio and that the author document itself exists and is published.

The typo in the field name is almost certainly your main issue. Fix that spelling mismatch and you should see your values coming through!

What values are coming null?
Have you considered that the data may be missing which is why you get null?
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
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). 🙂
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
user F
i am so sorry to bother you i have fixed the issue thanks for your precious time

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.

Was this answer helpful?