gatsby - 如何将变量从 'context' 传递给页面查询? | Gatsby

标签 gatsby

尝试将“上下文”变量传递给页面查询时出现错误,提示未提供该变量。

来 self 的 gatsby-node.js 文件:

createPage({
  path: `/portfolio/${pg.order}`,
  component: require.resolve("./src/pages/portfolio.js"),
  context: { pgNum: pg.order },
})

我的页面查询:

export const query = graphql`
  query($pgNum: Int!) {
    strapiPortfolioPages(order: { eq: $pgNum }) {
      layer
      widths
      ...
    }
  }

Gatsby 告诉我:“未提供所需类型“Int!”的变量“$pgNum”。

最佳答案

"Variable "$pgNum" of required type "Int!" was not provided."

这意味着你的 $pgNum 变量是不可空的,因为 Int! 因为感叹号 (!) 代表一个GraphQL 中的不可空(必需)值(有关更多信息,请查看 Schema and Types GraphQL documentation )。换句话说,您的查询将一个 null 值传递给一个不可为 null 的变量,因此您在某处有一个 null 值。

绕过它的一种简单方法(在检查您的值之后)是通过以下方式删除查询的可空性:

export const query = graphql`
  query($pgNum: Int) {
    strapiPortfolioPages(order: { eq: $pgNum }) {
      layer
      widths
      ...
    }
  }

关于gatsby - 如何将变量从 'context' 传递给页面查询? | Gatsby ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63560931/

相关文章:

javascript - 在 github 上创建 repo 时,Gatsby 开发服务器停止工作。理智, Gatsby 错误

reactjs - 使用 Graphql、React 的映射函数

node.js - 如何将 Gatsby 与 NodeJs Express 后端集成

css - 如何使用 material-ui 和 styled-components 定位按钮库

wordpress - 如何使用引导网格映射图像数组?

javascript - Gatsby `createPage` 正在生成具有相同数据的页面

javascript - 如果在 React 中单击链接,SlideMenu 不会关闭

javascript - 如何在 Gatsby 中使用带有富文本的内联图像?

使用 gatsby-plugin-react-i18next 时 Gatsby 找不到客户端路由

javascript - 使用 Helm 向 Gatsby 添加脚本标签会引发语法错误