Issue with fetching null values from schema and query in Slack thread
6 replies
Last updated: Sep 19, 2022
S
help Some values of schema is coming null while trying to fetch them
This is my schema of the cases
I am facing issue with date and refrence HERE IS MY QUERY
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}`,
})
},
},
}
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
K
What values are coming null?
Sep 19, 2022, 8:12 AM
K
Have you considered that the data may be missing which is why you get null?
Sep 19, 2022, 8:12 AM
K
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
K
Comparing your query with your schema, you query
refrencebut 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
S
now i changed my schema
AND CHANGE MY QUERY TO
AND THE RESPONSE NULL
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}`,
})
},
},
}
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,
}Sep 19, 2022, 8:33 AM
S
user F
i am so sorry to bother you i have fixed the issue thanks for your precious timeSep 19, 2022, 8:49 AM
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.