0

我正在尝试通过 Gatsby 中的 GraphQL 获取我的模拟 JSON 数据。响应显示正确的数据,但也显示两个空对象。为什么会这样?

我正在使用gatsby-transformer.json插件来查询我的数据并将gatsby-source-filesystem转换器指向我的 json 文件。

categories.json 我要开始工作的模拟文件:)

{
  "categories": [
    {
      "title": "DEZERTY",
      "path": "/dezerty",
      "categoryItems": [
        {
          "categoryName": "CUKRIKY",
          "image": "../../../../static/img/dessertcategories/cukriky.jpg"
        },
        {
          "categoryName": "NAHODNE",
          "image": "../../../../static/img/dessertcategories/nahodne.jpg"
        },
      ]
    },
    {
      "title": "CANDY BAR",
      "path": "/candy-bar",
      "categoryItems": [
        {
          "categoryName": "CHEESECAKY",
          "image": "../../../../static/img/dessertcategories/cheesecaky.jpg"
        },
        {
          "categoryName": "BEZLEPKOVÉ TORTY",
          "image": "../../../../static/img/dessertcategories/bezlepkove-torty.jpg"
        },
      ]
    }
  ]
}

GraphiQL 中的 GraphQL 查询

query Collections {
  allMockJson {
    edges {
      node {
        categories {
          categoryItems {
            categoryName
            image
          }
          title
          path
        }
      }
    }
  }
}

GraphiQL 给我的响应

{
  "data": {
    "allMockJson": {
      "edges": [
        {
          "node": {
            "categories": null
          }
        },
        {
          "node": {
            "categories": null
          }
        },
        {
          "node": {
            "categories": [
              {
                "categoryItems": [
                  {
                    "categoryName": "CHEESECAKY",
                    "image": "../../../../static/img/dessertcategories/cheesecaky.jpg"
                  },
                  {
                    "categoryName": "BEZLEPKOVÉ TORTY",
                    "image": "../../../../static/img/dessertcategories/bezlepkove-torty.jpg"
                  }
                ],
                "title": "DEZERTY",
                "path": "/dezerty"
              },
              {
                "categoryItems": [
                  {
                    "categoryName": "CUKRIKY",
                    "image": "../../../../static/img/dessertcategories/CUKRIKY.jpg"
                  },
                  {
                    "categoryName": "NAHODNE",
                    "image": "../../../../static/img/dessertcategories/NAHODNE.jpg"
                  }
                ],
                "title": "CANDY BAR",
                "path": "/candy-bar"
              }
            ]
          }
        }
      ]
    }
  }
}

我只希望得到DEZERTYCANDY BAR部分。为什么有空类别,我该如何解决?

提前致谢

4

1 回答 1

0

您的 JSON 在对象 DEZERTY 和 CANDY BAR 中包含语法错误。它默默地失败而不告诉你。试试这个json linter

Error: Parse error on line 12: }, Error: Parse error on line 25: },

再试一次。您的查询现在应该可以工作了。

您应该研究一个突出显示这些类型错误并节省您时间和挫败感的 IDE。

于 2019-08-22T07:05:12.280 回答