Troubleshooting undefined data when querying a new document type in a Next.js app with Sanity.io
40 replies
Last updated: Feb 10, 2023
K
Hey everyone! I've created a new document type called "Global"... I'm trying to query the data but for some reason I'm getting undefined. And strangely, I can seethe data in the terminal, but not the console when trying to log.....
Feb 10, 2023, 7:15 PM
R
Can you share more of the code around this?
Feb 10, 2023, 7:59 PM
K
Sure!
Feb 10, 2023, 8:24 PM
K
I have a schema called "global.ts that is being queried here:
// Global Settings export const globalSettingsQuery = groq` *[_type == 'global'][0] `
Feb 10, 2023, 8:25 PM
K
That is in
sanity.queries.ts
Feb 10, 2023, 8:26 PM
K
I'm exporting that type interface like so:
export interface GlobalSettings extends SanityBody { _type?: 'global' contactInformation?: { address?: string email?: string phoneNumber?: number } education?: { college?: string collegeLocation?: string dateEnded?: Date dateStarted?: Date degree?: string } homepageContent?: { heroImage: Image } personalInformation?: { bio?: string bioTitle?: string name?: string profilePic?: Image role?: string } socialMedia?: { social?: Social[] } }
Feb 10, 2023, 8:26 PM
K
Inside
sanity.client.tsI am calling the query:
export async function getGlobalSettings(): Promise<GlobalSettings | {}> { if (client) { return (await client.fetch(globalSettingsQuery)) || {} } return {} }
Feb 10, 2023, 8:27 PM
K
So I'm trying to grab this data, and be able to use in my own component that I'm making.
Feb 10, 2023, 8:28 PM
K
getGlobalSettings function is called in the
index.tsxfile:
const [globalSettings, settings, posts = []] = await Promise.all([ getGlobalSettings(), getSettings(), getAllPosts(), ])
Feb 10, 2023, 8:30 PM
K
So my thought pattern was to copy all the same functionality for the "settings" which worked out of the box. I've added additional schema work, and I want to query it in the same manner, but for some reason, "settings" has data, but "globalSettings" is undefined (and I am 100% sure there is data).
Feb 10, 2023, 8:31 PM
K
This is the first connection I have made to my own custom document type. Once I make this connection and understand how its working, I'm sure I can apply the same logic to my other data I'll need to retrieve.
Feb 10, 2023, 8:33 PM
K
Stepping away for 10 min to get my daughter, but I will be back! Any help here would be 100% appreciated...
Feb 10, 2023, 8:52 PM
R
I’m wrapping up a meeting but will jump back in here as soon as I’m able to.
Feb 10, 2023, 8:53 PM
K
For reference, I am using this nextjs starter: https://www.sanity.io/blog/nextjs-authoring-experience-with-sanity
Feb 10, 2023, 9:34 PM
R
Hmm, that code all looks ok. Is it possible that you’re not returning the data from this line:
const [globalSettings, settings, posts = []] = await Promise.all([ getGlobalSettings(), getSettings(), getAllPosts(), ])
Feb 10, 2023, 9:46 PM
K
Thats what I'm not sure of. I was thinking that I could copy the logic of the "settings" type to connect to my own document type, and as far as I can see I have all the parts.
Feb 10, 2023, 9:49 PM
K
When I console log "settings" and my "globalSettings" at the same place, only settings returns results. "globalSettings" is undefined
Feb 10, 2023, 9:52 PM
R
Can you share your entire
getStaticPropsfunction?
Feb 10, 2023, 9:55 PM
R
in the index page that is!
Feb 10, 2023, 9:55 PM
K
export const getStaticProps: GetStaticProps< PageProps, Query, PreviewData > = async (ctx) => { const { preview = false, previewData = {} } = ctx const [globalSettings, settings, posts = []] = await Promise.all([ getGlobalSettings(), getSettings(), getAllPosts(), ]) console.log('Another Test: ', globalSettings.socialMedia.social) return { props: { posts, settings, globalSettings, preview, token: previewData.token ?? null, }, } }
Feb 10, 2023, 9:56 PM
R
That looks correct, too! Can you share the entirety of your index page?
Feb 10, 2023, 10:03 PM
K
Yep!
Feb 10, 2023, 10:15 PM
K
import { PreviewSuspense } from '@sanity/preview-kit' import IndexPage from 'components/IndexPage' import { getAllPosts, getSettings, getGlobalSettings } from 'lib/sanity.client' import { Post, Settings, GlobalSettings } from 'lib/sanity.queries' import { GetStaticProps } from 'next' import { lazy } from 'react' const PreviewIndexPage = lazy(() => import('components/PreviewIndexPage')) interface PageProps { posts: Post[] settings: Settings social: GlobalSettings['socialMedia'] globalSettings: GlobalSettings preview: boolean token: string | null } interface Query { [key: string]: string } interface PreviewData { token?: string } export default function Page({ posts, settings, globalSettings, social, preview, token, }: PageProps) { console.log('Test: ', social) if (preview) { return ( <PreviewSuspense fallback={ <IndexPage loading preview posts={posts} settings={settings} social={social?.social} /> } > <PreviewIndexPage token={token} /> </PreviewSuspense> ) } return <IndexPage posts={posts} settings={settings} social={social?.social} /> } export const getStaticProps: GetStaticProps< PageProps, Query, PreviewData > = async (ctx) => { const { preview = false, previewData = {} } = ctx const [globalSettings, settings, posts = []] = await Promise.all([ getGlobalSettings(), getSettings(), getAllPosts(), ]) console.log('Another Test: ', globalSettings.socialMedia.social) return { props: { posts, settings, globalSettings, preview, token: previewData.token ?? null, }, } }
Feb 10, 2023, 10:15 PM
K
Now, there is likely some issues here. I've been making some edits trying different things out. 🥴
Feb 10, 2023, 10:15 PM
K
There's some things going on with
GlobalSettings["socialMedia"]that Im not so sure about...
Feb 10, 2023, 10:17 PM
R
Oh, I think I see it now! Your
Testconsole log is logging
social, which you’re destructuring in your
props. However, your
getStaticPropsisn’t returning
social. Social is actually on
globalSettings.socialMedia.social.
Feb 10, 2023, 10:19 PM
K
so I can just pass
globalSettings, and get rid of
social, right?
Feb 10, 2023, 10:24 PM
K
and just target it with the dot notation?
Feb 10, 2023, 10:24 PM
K
Yessss... haha, you're awesome!
Feb 10, 2023, 10:25 PM
K
I think the Sanity folk around here need to share a crypto address for tips....
Feb 10, 2023, 10:26 PM
R
Oh man that was a satisfying one to figure out!
Feb 10, 2023, 10:30 PM
K
Tell me about it... I've been sitting here banging my head around for hours. lol.... well, I kept coming back to it.
Feb 10, 2023, 10:32 PM
K
Now this makes sense for me. I can connect the rest of my document types up. 🙂
Feb 10, 2023, 10:32 PM
R
Everything else will be easy now!
Feb 10, 2023, 10:33 PM
K
I've spent a lot of time customizing the studio, so I'm excited to get the frontend parts hooked up.
Feb 10, 2023, 10:33 PM
K
This is what its lookin like:
Feb 10, 2023, 10:33 PM
R
Very nice! Love the theme.
Feb 10, 2023, 10:34 PM
K
Thanks!
Feb 10, 2023, 10:34 PM
K
Last question.... do you know what this error means on getStaticProps?
Feb 10, 2023, 10:55 PM
R
Hmm, that looks like a type error from Typescript, which I haven’t taken the dive into yet. I don’t think I can be more helpful than that!
Feb 10, 2023, 10:57 PM
K
Okay thanks!
Feb 10, 2023, 11:03 PM
Sanity.io – build remarkable experiences at scale
Sanity is a customizable solution that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.