Issue with fetching null values from schema and query in Slack thread
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:
- 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 }
);Check your Studio - make sure you've actually published the documents with values in those fields. If you're fetching with the default
publishedperspective (as of API version 2025-02-19), only published documents with published field values will appear.Verify the author reference exists - if the
authorfield 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!
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.