Watch a live product demo ๐Ÿ‘€ See how Sanity powers richer commerce experiences

Schema Preview Not Working After Update

5 replies
Last updated: Aug 11, 2021
Hi, After updating sanity to latest versions my schema (author) preview doesn't work anymore. the jobTitle comes back as undefined. Has anyone else encountered this problem after update?Here is my Authors.js code .

import { FiUser } from 'react-icons/fi'

import { createHeading, formatDate } from '../../helpers'

import SlugInput from 'sanity-plugin-better-slug'

import { isUniqueAcrossAllDocuments } from '../functions/isUniqueAcrossAllDocuments'

export default {
   name: 'author',
   title: 'Author',
   type: 'document',
   icon: FiUser,
   fields: [
      {
         name: 'name',
         title: 'Name',
         type: 'string'
      },
      {
         name: 'slug',
         title: 'Slug',
         type: 'slug',
         inputComponent: SlugInput,
         validation: (Rule) => Rule.required(),
         options: {
            source: 'name',
            maxLength: 100,
            basePath: '<http://localhost.com|localhost.com>',
            isUnique: isUniqueAcrossAllDocuments,
            slugify: (input) =&gt;
               input.toLowerCase().replace(/\s+/g, '-').slice(0, 100)
         }
      },
      {
         title: 'Job title',
         name: 'jobTitle',
         type: 'string',
         validation: (Rule) =&gt;
            Rule.required().error('This is required dear friend!'),
         options: {
            list: [
               // Others
               { title: 'Managing Partner', value: 'Managing Partner' },
               { title: 'Account Director', value: 'Account Director' },
               { title: 'Creative Director', value: 'Creative Director' },
               { title: 'Concepter', value: 'Concepter' },
               { title: 'Finance', value: 'Finance' },
               { title: 'Producer', value: 'Producer' },
               { title: 'Storyteller', value: 'Storyteller' },

               // Developers
               { title: 'Lead Developer', value: 'Lead Developer' },
               { title: 'Senior Developer', value: 'Senior Developer' },
               { title: 'Medior Developer', value: 'Medior Developer' },
               { title: 'Junior Developer', value: 'Junior Developer' },

               // Designers
               { title: 'Lead Designer', value: 'Lead Designer' },
               { title: 'Senior Designer', value: 'Senior Designer' },
               { title: 'Medior Designer', value: 'Medior Designer' },
               { title: 'Junior Designer', value: 'Junior Designer' },
               { title: 'Junior Creative', value: 'Junior Creative' }
            ]
         }
      },

      {
         name: 'bio',
         title: 'Bio',
         type: 'blockContent'
      },
      {
         name: 'authorImage',
         title: 'Author image',
         type: 'imageWithAltText'
      }
   ],

   preview: {
      select: {
         title: 'name',
         jobTitle: 'jobTitle',
         media: 'authorImage'
      },
      prepare(selection) {
         const { title, jobTitle } = selection

         return Object.assign({}, selection, {
            title: title,
            subtitle: jobTitle
         })
      }
   }
}
Jul 27, 2021, 8:27 AM
Hi,
user E
. I'm experiencing the exact same problem. I've been trying to isolate it and so far I've discovered the same issue in a clean sanity studio when you have a
string
field with a
list
option.
Jul 27, 2021, 8:29 AM
Here is my complete schema file that reproduces the problem. You can see that I'm setting a subtitle using
preview.select.subtitle
but when I set it to
pickSomething
it is blank.

// First, we must import the schema creator
import createSchema from 'part:@sanity/base/schema-creator'

// Then import schema types from any plugins that might expose them
import schemaTypes from 'all:part:@sanity/base/schema-type'

const obj = {
	title: "Obj",
	name: "obj",
	type: "object",
	fields: [
		{
			title: "Plain text",
			name: "plain",
			type: "string"
		},
		{
			title: "Pick something",
			name: "pickSomething",
			type: "string",
			options: {
				list: [
					{ title: "Alpha", value: "a" },
					{ title: "Bravo", value: "b" },
					{ title: "Charlie", value: "c" },
					{ title: "Delta", value: "d" },
					{ title: "Echo", value: "e" },
					{ title: "Foxtrot", value: "f" },
				]
			}
		}
	],
	preview: {
		select: {
			title: "plain",
			subtitle: "pickSomething"
		}
	}
}

const doc = {
	title: "Doc",
	name: "doc",
	type: "document",
	fields: [
		{
			title: "Pickers",
			name: "pickers",
			type: "array",
			of: [{ type: "obj" }]
		}
	]
}

// Then we give our schema to the builder and provide the result to Sanity
export default createSchema({
  // We name our schema
  name: 'default',
  // Then proceed to concatenate our document type
  // to the ones provided by any plugins that are installed
  types: schemaTypes.concat([
    /* Your types here! */
    obj,
    doc
  ]),
})
Jul 27, 2021, 8:31 AM
There seems to be an open issue for this bug already:
https://github.com/sanity-io/sanity/issues/2631
Jul 27, 2021, 8:35 AM
Perfect
user A
! Thanks for letting me know. I'll update sanity right away
Aug 11, 2021, 2:06 PM
Can confirm that the problem seems to be fixed ๐Ÿ™‚
Aug 11, 2021, 2:11 PM

Sanityโ€“ build remarkable experiences at scale

The Sanity Composable Content Cloud is the 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.

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
After adding the subtitle and running this code npm run graphql-deploy It does nothingSep 15, 2020
how to limit a reference to just one entry in Studio reference input side versus the default as-many-entries-as-you-fill-in-an-array...Sep 18, 2020
Is it possible to fetch more than one "_type" using GROQ?Nov 2, 2020
I want to add a view with the Structure builder (S.view.component) where I list similar documents based on the title. What...Sep 23, 2020
Is there a structure builder example where the format of each preview for the document list is modified?Feb 3, 2021
I have an array of references to a country schema type but it always just returns NULL values for meJan 30, 2021
Hi, I need help with a query for getting the url of an image asset. Here is what I've been trying, but I only get the _ref...Dec 1, 2020
Sanity UI looks brilliant :smiley: Is something like the current date picker possible at the moment? Iโ€™m not sure if anicon...Dec 21, 2020
Hey everyone. I have been coding and may have potentially accidentally deleted something. Does anyone know how to resolve...Dec 26, 2020
Hello everyone and happy new year :raised_hands::skin-tone-2:, I have a problem with outputting Portable Text :disappointed:...Jan 1, 2021

Related contributions

Clean Next.js + Sanity app
- Template

Official(made by Sanity team)

A clean example of Next.js with embedded Sanity ready for recomposition.

Cody Olsen
Go to Clean Next.js + Sanity app

Blog with Built-in Content Editing
- Template

Official(made by Sanity team)

A Sanity-powered blog with built-in content editing and instant previews.

Go to Blog with Built-in Content Editing