References showing as "untitled" in Sanity array - how to add preview?

34 replies
Last updated: Jan 15, 2021
Hello everyone,I was wondering if someone could help me with my problem?
Currently in a schema I defined fields of a custom type "section"(type :'object'). this schema contains an array of references. When I now try to add references, they are shown as untitled. how can i change that?

export default {
    title: 'Versprechen Page',
    name: 'promise',
    type: 'document',
    ...
    fields: [
        {
            name: 'section1',
            type: 'section',
            fieldset: 'sectionGroup0'
        },
        ... ]
}

export default {
    title: 'Abschnitt',
    name: 'section',
    type: 'document',
    fields: [
        ...
        {
            title: 'Content',
            name: 'content',
            type: 'array',
            of: [
                {
                    type: 'reference',
                    to: [
                        {type: 'pictureAndText'},
                        {type: 'titleAndText'},
                        {type: 'collection'},
                        {type: 'link'}
                    ]
                }
            ]
        },
       ...
    ],
}

AI Update

The issue you're seeing is that the referenced documents don't have a preview configuration set up, so Sanity defaults to showing "Untitled". You need to add a preview configuration to each of the referenced document types (pictureAndText, titleAndText, collection, and link).

Here's how to fix it by adding preview configuration to your referenced schemas:

// pictureAndText.js (example)
export default {
    title: 'Picture and Text',
    name: 'pictureAndText',
    type: 'document',
    fields: [
        {
            name: 'title',
            type: 'string'
        },
        {
            name: 'picture',
            type: 'image'
        },
        // ... other fields
    ],
    preview: {
        select: {
            title: 'title',
            media: 'picture'
        }
    }
}

The preview object has two parts:

  • select: Specifies which fields to use in the preview
  • prepare (optional): Transforms the data before display

For a basic setup, just using select with a title field is usually enough. If your documents don't have a field literally named "title", adjust it to whatever field makes sense:

preview: {
    select: {
        title: 'name',  // or whatever your main identifying field is
        media: 'image'   // optional: shows a thumbnail
    }
}

If you need more control over how the preview displays, you can add a prepare function:

preview: {
    select: {
        title: 'title',
        subtitle: 'description',
        media: 'image'
    },
    prepare({title, subtitle, media}) {
        return {
            title: title || 'Untitled',
            subtitle: subtitle,
            media: media
        }
    }
}

Apply this pattern to all four of your referenced document types (pictureAndText, titleAndText, collection, and link), and the references in your array will show meaningful titles instead of "Untitled".

Note: One small issue in your code - you've defined section as type: 'document', but since you're using it as a field type within another document, it should probably be type: 'object' instead. Documents are top-level content types, while objects are embedded within documents.

Show original thread
34 replies
have you tried adding a preview object? https://www.sanity.io/docs/previews-list-views

preview: {
  select: {
    title: "myFieldToUse"
  },
},
yea i did
nothing changed
export default{
    title: 'Titel und Text',
    name: 'titleAndText',
    type: 'document',
    fields: [
        {
            title: 'Title',
            name: 'title',
            type: 'string'
        },
        {
            title: 'Text',
            name: 'text',
            type: 'array',
            of:[
                {type:'block'}
            ]
        },
        {
            title: 'Custom Type',
            name: 'custom',
            type: 'string'
        },
    ],
    preview: {
        select: {
            title: 'title',
        },
        prepare(selection) {
            const {title} = selection
            return {
                title: title,
            }
        },
    }
}

thats the reference id like to link in the array
have you tried adding the preview inside the array?

to: [
  {
    type: 'pictureAndText'
    preview: {
      title: title
    }
  },
]

do the titles inside the title and text documents have a value at all?
(btw you can omit the prepare function if you're not manipulating the title)
yea they do
and it doesnt work with the title inside the array
strange … i've copied your setup and it works here
and when u chose one it shows?
the title
have you published your documents?
I can reproduce it when I publish a document without a title and fill it - and then forget publishing.
i published everything
which sanity version are you on?
I just upgraded and am getting a lot of untitled references as well!
upgraded today
so its a bug in sanity?
user Y
user M
seems something is off with the references since todays update
might be this thing https://github.com/sanity-io/sanity/pull/2212 ("merging this should be fairly low risk in terms of breaking existing functionality.")
so ill wait on a fix then?
or downgrade
whats the command to downgrade to previous version?
update sanity/base dependency in package.json with a fixed version before this release and run
yarn
or
npm install
again
"@sanity/base": "2.1.4",
awesome thanks
Thanks both 🙌 we're looking into this and will release a fix asap. Sorry for the confusion, Tim!
No problem, GOOD SUPPORT!
a fix is out, works fine now after upgrading to 2.1.6 (
sanity upgrade
)
We just released
v2.1.6
, which should fix the issue 🙂
awesome thank you
Ah, this was my issue too. Upgraded and fixed. Thanks

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?