You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Sanity Schema中批量导入导出默认文件目录?

在Sanity中批量导入文档Schema的正确方式

你遇到的<unnamed_type_@_index_1>错误,核心原因是传给schemaTypes.concat()的不是Schema对象数组,而是一个包含Schema的对象。Sanity无法识别对象结构的类型定义,因此抛出命名错误。

下面是两种可行的批量导入方案:

方案一:直接导出Schema数组

修改/documents/index.js,直接导出所有Schema组成的数组:

// /documents/index.js
import author from './author'
import blog from './blog'

// 直接导出数组
export default [author, blog]

然后在schema.js中直接导入这个数组并拼接:

// schema.js
import createSchema from 'part:@sanity/base/schema-creator'
import schemaTypes from 'all:part:@sanity/base/schema-type'

// 导入整个目录的Schema数组
import documents from './documents/'

export default createSchema({
  name: 'default',
  // 直接拼接数组即可
  types: schemaTypes.concat(documents),
})

方案二:将导出的对象转为数组

如果想保持index.js导出单个对象的写法,在schema.js中用Object.values()把对象转为Schema数组:

// /documents/index.js(两种写法都支持)
// 写法1
export { default as author } from './author'
export { default as blog } from './blog'

// 写法2
import author from './author'
import blog from './blog'
export { author, blog }

然后修改schema.js:

// schema.js
import createSchema from 'part:@sanity/base/schema-creator'
import schemaTypes from 'all:part:@sanity/base/schema-type'

import * as documents from './documents/'

export default createSchema({
  name: 'default',
  // 把对象的值转为数组再拼接
  types: schemaTypes.concat(Object.values(documents)),
})

关键注意点

无论用哪种方案,确保每个Schema文件(比如author.js、blog.js)都定义了正确的name字段,示例:

// /documents/author.js
export default {
  name: 'author', // 必须包含该字段
  title: '作者',
  type: 'document',
  // ...其他字段定义
}

如果缺少name字段,Sanity依然会抛出未命名类型的错误。

内容的提问来源于stack exchange,提问作者GʀᴜᴍᴘʏCᴀᴛ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 18:01:16