Gatsby createPages with GraphQL在Graphiql返回字符串时返回Object:null原型

Gatsby createPages with GraphQL在Graphiql返回字符串时返回Object:null原型,graphql,gatsby,strapi,Graphql,Gatsby,Strapi,我正在盖茨比用Strapi的slug通过Graphql建立createPages 我的问题是: query { allStrapiPosts { nodes { slug } } } GraphiQL返回: { "data": { "allStrapiPosts": { "nodes": [ { "slug": "/pizza-struggles-to

我正在盖茨比用Strapi的slug通过Graphql建立createPages

我的问题是:

query {
      allStrapiPosts {
        nodes {
          slug
        }
      }
    }
GraphiQL返回:

{
  "data": {
    "allStrapiPosts": {
      "nodes": [
        {
          "slug": "/pizza-struggles-today"
        },
        {
          "slug": "/raining-in-paradise"
        },
        {
          "slug": "/blog-title"
        }
      ]
    }
  }
}
My gatsby-node.js:

const path = require("path")

exports.createPages = async ({ graphql, actions }) => {
  const { createPage } = actions

  const { data } = await graphql(`
    query {
      allStrapiPosts {
        nodes {
          slug
        }
      }
    }
  `)
  console.log(data)

  data.allStrapiPosts.nodes.forEach(({ node }) => {
    createPage({
      path: `blog${node.slug}`,
      component: path.resolve("./src/Templates/Post-Template.js"),
      context: {
        slug: node.slug,
      },
    })
  })
}
以下是来自终端的错误:

[Object: null prototype] {
  allStrapiPosts: [Object: null prototype] {
    nodes: [
      [Object: null prototype],
      [Object: null prototype],
      [Object: null prototype]
    ]
  }
}

 ERROR #11321  PLUGIN

"gatsby-node.js" threw an error while running the createPages lifecycle:

Cannot read property 'slug' of undefined

  17 |   data.allStrapiPosts.nodes.forEach(({ node }) => {
  18 |     createPage({
> 19 |       path: `blog${node.slug}`,
     |                         ^
  20 |       component: path.resolve("./src/Templates/Post-Template.js"),
  21 |       context: {
  22 |         slug: node.slug,

File: gatsby-node.js:19:25



  TypeError: Cannot read property 'slug' of undefined

  - gatsby-node.js:19
    /Users/jeffstahlnecker/Developer/mxc-blog/gatsby-node.js:19:25

  - Array.forEach

  - gatsby-node.js:17 Object.exports.createPages
    /Users/jeffstahlnecker/Developer/mxc-blog/gatsby-node.js:17:29

  - task_queues.js:94 processTicksAndRejections
    internal/process/task_queues.js:94:5


failed createPages - 0.046s
我在盖茨比其他地方的询问效果很好。例如,我有一个类似的查询,创建了一个博客帖子列表,这个查询可以正常工作

昨晚花了几个小时试图解决这个问题。非常感谢您的帮助

从gastby-config.js:

{
      resolve: `gatsby-source-strapi`,
      options: {
        apiURL: `https://cms.mxc.org:443`,
        queryLimit: 1000, // Default to 100
        contentTypes: [`posts`],
        //If using single types place them in this array.
        // singleTypes: [`home-page`, `contact`],
        // Possibility to login with a strapi user, when content types are not publically available (optional).
        loginData: {
          identifier: "",
          password: "",
        },
      },
    },

您的
gatsby config.js
中的
gatsby源代码strapi
是什么样子?您是否确定希望使用
节点
而不是
边{node{slug}
forEach((node)
?而是……但是您是否发现有
{node}
正在尝试从迭代元素中提取
节点
,而不是将整个元素用作
节点
?是的@xadm您是对的。我建议OP读取
数组。forEach
文档,因为很明显,为了访问
slug
,不需要尝试访问
节点
。你的
gatsby config.js
中的
gatsby source strapi
是什么样子的?你确定你想使用
节点
而不是
边{node{slug}
forEach((node)
?而是……但是你发现有
{node}
正在尝试从迭代元素中提取
节点
,而不是将整个元素用作
节点
?是的@xadm您是对的。我建议OP读取
数组。forEach
文档,因为很明显,为了访问
slug
,不需要尝试访问
节点