Sanity Studio Vision查询报错:unexpected token ","问题求助
问题
我正在学习React Native课程,采用Sanity作为后端服务,已完成featured、restaurant、dish及category的schema配置并在Sanity Studio中填充数据。在Vision面板执行以下GROQ查询时:
*[_type == "featured"]{ ..., restuarants[]=> { ..., dishes[]=> , type=> { name } }, }
出现错误提示:unexpected token ",", expected expression。我已对照教程代码检查、谷歌搜索,但问题已持续2天仍未解决,作为Sanity查询新手,请求协助排查问题。
附上我的schema代码:
featured schema
export default { name: 'featured', title: 'featured Menu Categories', type: 'document', fields: [ { name: 'name', type: 'string', title: 'Featured category name', validation: (Role) => Role.required(), }, { name: 'short_description', type: 'string', title: 'Short description', validation: (Role) => Role.max(200), }, { name: 'restuarants', type: 'array', title: 'Restuarants', of: [{ type: 'reference', to: [{ type: 'restuarant' }] }], }, ], };
restaurant schema
export default { name: 'restaurant', title: 'Restuarant', type: 'document', fields: [ { name: 'name', type: 'string', title: 'Restuarant name', validation: (Role) => Role.required(), }, { name: 'short_description', type: 'string', title: 'Short description', validation: (Role) => Role.max(200), }, { name: 'image', type: 'image', title: 'Image of the Restuarant', }, { name: 'lat', type: 'number', title: 'latitude of the restaurant', }, { name: 'long', type: 'number', title: 'longitude of the restaurant, }, { name: 'address', type: 'string', title: 'Address of the Restuarant', validation: (Role) => Role.required(), }, { name: 'rating', type: 'number', title: 'Enter a rating of (1 - 5)', validation: (Role) => Role.required() .min(1) .max(5) .error('please enter a number between 1 - 5'), }, { name: 'type', type: 'string', title: 'Category', validation: (Role) => Role.required(), type: 'reference', to: [{ type: 'category' }], }, { name: 'dishes', type: 'array', title: 'Dishes', of: [{ type: 'reference', to: [{ type: 'dish' }] }], }, ], };
问题排查与解决
1. GROQ查询语法错误(直接报错原因)
查询中dishes[]=> ,这一行末尾多了冗余逗号,且type=>的缩进结构不符合GROQ语法规范。修正后的查询如下:
*[_type == "featured"]{ ..., restuarants[]=> { ..., dishes[]=>, type=> { name } } }
如果需要获取dishes的全部字段,dishes[]=>后无需逗号;若仅需部分字段,需写成dishes[]=> { 字段名1, 字段名2 }。
2. Restaurant Schema中的两处语法错误
虽不直接导致本次查询报错,但会引发Sanity Studio运行异常,建议同步修正:
long字段的title值缺少闭合引号,修正后:{ name: 'long', type: 'number', title: 'longitude of the restaurant', },type字段重复定义type属性,需删除冗余的type: 'string',保留引用类型定义:{ name: 'type', title: 'Category', validation: (Role) => Role.required(), type: 'reference', to: [{ type: 'category' }], },
3. 拼写一致性问题
注意到你在schema中将restaurant拼写成restuarant(少一个字母a),虽当前查询可匹配,但后续开发易因拼写不一致引发问题,建议统一修正为restaurant(含字段名、文档类型名等)。
内容的提问来源于stack exchange,提问作者MegaMindTheCoder
相关产品推荐
相关产品推荐

