Joe*_*ven 5 javascript json graphql gatsby
我正在尝试查询一些可能包含空数组的 JSON 数据,JSON 文件将可以通过 CMS 访问,因此它必须支持为空,但是,当 gatsby 尝试查询空数组时,它会抛出错误(大概是因为我'我试图查询数组内对象内的项目)。
Gatsby 有没有办法查询 JSON 文件但期望数组为空?(使里面的对象可选?)。
这是我在代码中遇到的困境:
careers.json
{
"title": "Careers",
"subtitle": "Looking for an exciting new career change?",
"jobs": [
{
"title": "Job 01",
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Sed posuere consectetur est at lobortis. Maecenas faucibus mollis interdum. Curabitur blandit tempus porttitor."
}
]
}
Run Code Online (Sandbox Code Playgroud)
jobs当没有任何作业时,上面的 JSON 属性也将是一个空数组,如下所示:
{
"title": "Careers",
"subtitle": "Looking for an exciting new career change?",
"jobs": []
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试在 Gatsby 页面中查询此内容时,它会抛出错误:
export const query = graphql`
query {
careersJson {
title
subtitle
jobs {
title
description
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
Cannot query field "jobs" on type "CareersJson".
GraphQL request:27:13
26 | subtitle
27 | jobs {
| ^
28 | title
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1004 次 |
| 最近记录: |