Debugging an error with initial value templates in Sanity Studio

15 replies
Last updated: Oct 20, 2021
Hey everyone,I’ve got a rather exotic bug/error:

Target value is neither indexable or an object.
When trying to select a page as a reference. Seems to occur with a specific page. Oddly enough the issue doesn’t happen for me but only for one of my colleagues so not being able to recreate is making it tough to debug.
I can’t seem to find anything about this error and wondered if anyone else has seen something like this before?
AI Update

This is indeed an exotic error! "Target value is neither indexable or an object" is not commonly documented, but the fact that it only affects one colleague is a big clue - this typically points to a local environment or cache issue.

Most Likely Causes

1. Schema/Cache Mismatch (Most Common) Since it's colleague-specific, they likely have stale cached data. The reference field is trying to read a document structure that doesn't match what it expects.

Have your colleague try:

  • Delete the .sanity folder in your project directory
  • Hard refresh the browser (Ctrl+Shift+R / Cmd+Shift+R)
  • Clear browser cache and localStorage for the Studio URL
  • Run sanity schema deploy to sync schemas
  • Try opening Studio in an incognito/private window

2. Corrupted Document Data That specific page might have malformed data. This happens when schemas change but documents aren't migrated, or if there's a null/unexpected value where the reference preview expects an object.

To investigate, use Vision (GROQ query tool):

*[_id == "problematic-page-id"][0]

Look for any fields with unexpected values, missing required fields, or null where objects should be.

3. Custom Preview Configuration Issue If your page schema has a custom preview configuration, it might be trying to access nested fields that don't exist on that specific document:

preview: {
  select: {
    title: 'title',
    subtitle: 'someField.nestedProperty' // If this path is broken
  }
}

4. Dependency Version Mismatch Your colleague might have different package versions:

  • Have them delete node_modules and run npm install fresh
  • Verify they're on the same Node.js version as you
  • Check for any local Studio configuration overrides

Quick Fix Sequence

  1. Close Studio completely
  2. Delete browser cache/localStorage
  3. Delete node_modules and reinstall
  4. Restart dev server
  5. Open in incognito window

If none of this works, the document data itself is likely the culprit - you'll need to inspect it in Vision and potentially migrate/fix the malformed fields on that specific page.

Hey User! Haven't seen this one come up yet. Which version of the Studio are you running?
Is there anything different about the particular page you're getting this with that could be causing it?
@sanity/cli                     2.21.4 (latest: 2.21.5)
@sanity/base                    2.20.0 (latest: 2.21.5)
@sanity/block-content-to-react   3.0.0 (up to date)
@sanity/color-input             2.20.0 (latest: 2.21.5)
@sanity/components              2.14.0 (up to date)
@sanity/core                    2.20.0 (latest: 2.21.5)
@sanity/dashboard               2.20.0 (latest: 2.21.5)
@sanity/default-layout          2.20.0 (latest: 2.21.5)
@sanity/default-login           2.19.0 (latest: 2.21.4)
@sanity/desk-tool               2.20.0 (latest: 2.21.5)
@sanity/production-preview      2.15.0 (up to date)
@sanity/vision                  2.20.0 (latest: 2.21.5)
Actually have just found a way to recreate. It seems to happen only when starting a page from an
initial value template
@sanity/cli                     2.21.4 (latest: 2.21.5)
@sanity/base                    2.20.0 (latest: 2.21.5)
@sanity/block-content-to-react   3.0.0 (up to date)
@sanity/color-input             2.20.0 (latest: 2.21.5)
@sanity/components              2.14.0 (up to date)
@sanity/core                    2.20.0 (latest: 2.21.5)
@sanity/dashboard               2.20.0 (latest: 2.21.5)
@sanity/default-layout          2.20.0 (latest: 2.21.5)
@sanity/default-login           2.19.0 (latest: 2.21.4)
@sanity/desk-tool               2.20.0 (latest: 2.21.5)
@sanity/production-preview      2.15.0 (up to date)
@sanity/vision                  2.20.0 (latest: 2.21.5)
Actually have just found a way to recreate. It seems to happen only when starting a page from an
initial value template
Interesting! What does the initial value template look like?
Ahh actually I think it’s an error creating the page from the initial value template as opposed to selecting the referenced object.
So the initial value template is actually created asynchronously from a sanity document. This way the team can update the initial value templates themselves so I think I am probably doing something funky in that part. 👀 Will investigate further.
Thanks for your help. :rubberduck:
Some of the fields in the initial value template are
undefined
. Would this cause issues?
It could be that things aren't being returned since the templates are created asynchronously. Can you share the code for how they're being created?
import T from '@sanity/base/initial-value-template-builder'
import sanityClient from 'part:@sanity/base/client'
import { IoIosDesktop } from 'react-icons/io'

const query = '*[_type == "pageTemplates" && title == $title]'
const getTemplate = async (templateTitle: string) => {
  const template = await sanityClient.fetch(
    query,
    { title: templateTitle },
  );
  const {
    title,
    header,
    action,
    type,
    topic,
    seoMeta,
    sections,
    parentPage,
  } = template[0]
  return {
    title,
    header,
    action,
    type,
    topic,
    seoMeta,
    sections,
    parentPage,
  }
}
export default [
  ...T.defaults(),
  T.template({
    id: 'page-lander-qa',
    title: 'QA Lander',
    schemaType: 'pages',
    icon: IoIosDesktop,
    value: async () => await getTemplate('QA Lander'),
  }),
  T.template({
    id: 'page-thank-you',
    title: 'Thank You',
    schemaType: 'pages',
    icon: IoIosDesktop,
    value: async () => await getTemplate('Thank You'),
  }),
]
So it fetches the
pageTemplates
which has virtually the same schema as the
pages
. Then creates a new page from that template.
Can confirm that it doesn’t error when I remove all the undefined schemas
Can confirm that it doesn’t error when I remove all the undefined schemas
Got it. Is it possible that the document you're fetching doesn't have those values defined and published?
Yeah, that’s it so the template in this example isn’t adding any sections. I guess I need to omit any keys that return undefined values

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?