How to display referenced technology names in Sanity preview subtitle?

4 replies
Last updated: Feb 9, 2024
defineField({
			name: 'technologies',
			title: 'Technologies',
			type: 'array',
			of: [{ type: 'reference', to: [{ type: 'technology' }] }]
		}),
I am trying to show chosen technologies in the subtitle field but dont know how to. Any tips?


preview: {
		select: {
			customerName: 'customer.name',
			customerImage: 'customer.image',
			technologies: 'technologies.name' // want the names of the technology.
		},
		prepare(selection) {
			const { customerName, customerImage, technologies } = selection;
			const subtitleTechnologies = technologies ? technologies.slice(0, 3).join(', ') : '';

			return {
				title: customerName,
				subtitle: subtitleTechnologies,
				media: customerImage
			};
		}
	}
AI Update

The issue is that when you're selecting from an array of references, you need to use special syntax to dereference them. Your current approach technologies: 'technologies.name' won't work because technologies is an array of references, not a single reference.

For arrays of references, you need to access individual items by index and dereference each one. Here's how to fix it:

preview: {
  select: {
    customerName: 'customer.name',
    customerImage: 'customer.image',
    tech0: 'technologies.0.name',  // First technology
    tech1: 'technologies.1.name',  // Second technology
    tech2: 'technologies.2.name'   // Third technology
  },
  prepare(selection) {
    const { customerName, customerImage, tech0, tech1, tech2 } = selection;
    
    // Filter out undefined values and join
    const technologies = [tech0, tech1, tech2].filter(Boolean);
    const subtitleTechnologies = technologies.join(', ');

    return {
      title: customerName,
      subtitle: subtitleTechnologies,
      media: customerImage
    };
  }
}

The key points:

  1. Use numeric indices - Access array items using technologies.0.name, technologies.1.name, etc. to get the name field from each referenced technology document
  2. Select multiple items - Since you can't directly map over arrays in the select object, you need to explicitly select each item you want (up to the limit you need)
  3. Filter in prepare - Use .filter(Boolean) to remove any undefined values if there are fewer than 3 technologies

This approach is documented in the preview configuration documentation, where arrays are accessed using numeric indices like images.0.asset.

If you need to display more than a few technologies or a variable number, you might want to consider using a custom preview component instead, which gives you more flexibility for complex scenarios.

Show original thread
4 replies
It looks like you’re on the right track. What’s it currently showing?
[object object] for each technology
OK, I see the problem now. You’re going to have to adjust your select to match this pattern.
Thank you!

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?