🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

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?
Oct 20, 2021, 10:24 PM
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?
Oct 20, 2021, 10:37 PM
@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
Oct 20, 2021, 10:40 PM
@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
Oct 20, 2021, 10:40 PM
Interesting! What does the initial value template look like?
Oct 20, 2021, 10:44 PM
Ahh actually I think it’s an error creating the page from the initial value template as opposed to selecting the referenced object.
Oct 20, 2021, 10:44 PM
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.
Oct 20, 2021, 10:45 PM
Thanks for your help. :rubberduck:
Oct 20, 2021, 10:46 PM
Some of the fields in the initial value template are
undefined
. Would this cause issues?
Oct 20, 2021, 10:51 PM
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?
Oct 20, 2021, 11:05 PM
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'),
  }),
]
Oct 20, 2021, 11:06 PM
So it fetches the
pageTemplates
which has virtually the same schema as the
pages
. Then creates a new page from that template.
Oct 20, 2021, 11:08 PM
Can confirm that it doesn’t error when I remove all the undefined schemas
Oct 20, 2021, 11:12 PM
Can confirm that it doesn’t error when I remove all the undefined schemas
Oct 20, 2021, 11:12 PM
Got it. Is it possible that the document you're fetching doesn't have those values defined and published?
Oct 20, 2021, 11:12 PM
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
Oct 20, 2021, 11:13 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?