Issue with setup for preview in a referenced type is resolved

3 replies
Last updated: Mar 22, 2023
I am having this issue, can someone please take a look and let me know what I could possibly have done wrong? I already checked for all the spellings and potential hidden special characters, I will attach some more references in the comment sections


SOLVED - leaving this here for anyone with similar issues, it looks like my setup for preview in the referenced type is wrong, I fixed it and it is working now
Mar 22, 2023, 2:03 PM
import {defineField, defineType} from 'sanity'
import {MdAttachFile as icon} from 'react-icons/md'

export default defineType({
  name: 'publication',
  title: 'Publication',
  type: 'document',
  icon,
  fields: [
    defineField({
      name: 'title',
      title: 'Title',
      type: 'string',
    }),
    defineField({
      name: 'slug',
      title: 'Slug',
      type: 'slug',
      options: {
        source: 'title',
        maxLength: 100,
      },
    }),
    defineField({
      name: 'truckIdentifiers',
      title: 'Truck Identifiers',
      type: 'array',
      of: [
        {
          type: 'reference',
          to: [
            {
              type: 'truckIdentifier',
            },
          ],
        },
      ],
    }),
    defineField({
      name: 'releaseDate',
      title: 'Release date',
      type: 'datetime',
    }),
    defineField({
      name: 'overview',
      title: 'Overview',
      type: 'blockContent',
    }),
    defineField({
      name: 'featuredImage',
      title: 'Featured Image',
      type: 'image',
      options: {
        hotspot: true,
      },
    }),
  ],
  preview: {
    select: {
      title: 'title',
      date: 'releaseDate',
      media: 'poster',
      // castName0: 'castMembers.0.person.name',
      // castName1: 'castMembers.1.person.name',
      // truckIdentifier0: 'truckIdentifiers.0.truckIdentifier.name',
    },
    prepare(selection) {
      const year = selection.date && selection.date.split('-')[0]
      // const Identifiers = [selection.truckIdentifier0].filter(Boolean).join(', ')

      return {
        title: `${selection.title} ${year ? `(${year})` : ''}`,
        date: selection.date,
        // subtitle: Identifiers,
        media: selection.media,
      }
    },
  },
})
Mar 22, 2023, 2:03 PM
import {defineField, defineType} from 'sanity'
// import {MdPerson as icon} from 'react-icons/md'

export default defineType({
  name: 'truckIdentifier',
  title: 'Truck Identifier',
  type: 'document',
  // icon,
  fields: [
    defineField({
      name: 'name',
      title: 'Identifier Name',
      type: 'string',
    }),
    defineField({
      name: 'slug',
      title: 'Slug',
      type: 'slug',
      options: {
        source: 'name',
        maxLength: 100,
      },
    }),
    defineField({
      name: 'image',
      title: 'Image',
      type: 'image',
      options: {
        hotspot: true,
      },
    }),
    defineField({
      name: 'truckModels',
      title: 'Truck Models',
      type: 'array',
      of: [
        {
          type: 'reference',
          to: [
            {
              type: 'truckModel',
            },
          ],
        },
      ],
    }),
  ],
  preview: {
    select: {
      title: 'name',
      subtitle: 'truckModels[0].name',
      media: 'image',
    },
  },
})
Mar 22, 2023, 2:04 PM
Thanks for sharing your solution!
Mar 22, 2023, 4:32 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?