How to save multi-select category array from frontend to Sanity schema?
Looking at your code, the issue is that you're defining the categories field in your Sanity schema correctly, but you need to make sure you're actually sending the category data in the right format when you create/upload the document to Sanity.
Your Sanity schema looks good:
{
title: 'Categories',
name: 'categories',
type: 'array',
of: [{ type: 'string' }],
options: {
layout: 'tags'
}
}The problem is in how you're handling the data from CreatableSelect. When you call setCategory(e), the e parameter contains an array of option objects like [{value: 'Garden', label: 'Garden'}, {value: 'Kitchen', label: 'Kitchen'}], but Sanity expects just an array of strings like ['Garden', 'Kitchen'].
Fix your handleOnChange function:
const handleOnChange = (e) => {
// e is an array of option objects like [{value: 'Garden', label: 'Garden'}, ...]
// Extract just the values for Sanity
const categoryValues = e ? e.map(option => option.value) : [];
setCategory(categoryValues); // This should be ['Garden', 'Kitchen'] - array of strings
setselectedValues({ selected: e }); // Keep the full objects for the UI display
};When you submit to Sanity, make sure your mutation looks like:
// When creating/patching the document
const doc = {
_type: 'yourDocumentType',
// ... other fields
categories: category, // This should be ['Garden', 'Kitchen', 'Red'] - just strings
};
// Using the Sanity client
client.create(doc)
.then(response => {
console.log('Document created:', response);
})
.catch(err => {
console.error('Upload failed:', err.message);
});The key issue is that CreatableSelect passes the entire option objects in the onChange event, but Sanity's array of strings expects just the string values. By using .map(option => option.value), you extract just the values.
If you're still getting errors, check your browser console for the specific error message - it will tell you exactly what Sanity is receiving vs what it expects. Also make sure your Sanity client is properly configured with your project ID and dataset.
Show original thread6 replies
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.