盖茨比无法生成正确的页面

时间:2020-09-27 17:12:16

标签: reactjs graphql gatsby contentful

我遇到了无法生成blog.js页面的问题。

成功

  • 我本可以连接gatsby并获得满意的结果,并获得有关 主页(http:// localhost:8000)

失败

  • 当我单击“更多”按钮时,单击“ Card on(localhost:8000)”,然后 切换到另一个页面(localhost:8000 / blog /( 创建于内容上)),但未成功执行,并且404页具有 出现了(唯一的炉渣已经改变并出现了我期望的样子)。

My repo

我的部分解决方案(但不希望这样做)

・ Transfrer文件(./src/templates/blog.js)→(./src/pages/blog.js)可能显示页面(http:// localhost:8000 / blog ),但我不知道为什么),下面的错误消失了,但无法获取数据(有内容)。

错误

> warn The GraphQL query in the non-page component
> "C:/Users/taiga/Github/Gatsby-new-development-blog/my-blog/src/templates/blog.js"
> will not Exported queries are only executed for Page components. It's
> possible you're trying to create pages in your gatsby-node.js and
> that's failing for some reason.
> 
> If the failing component(s) is a regular component and not intended to
> be a page component, you generally want to use a <StaticQuery>
> (https://gatsbyjs.org/docs/static-query) instead of exporting a page
> query.

下一个错误

  • 无法读取未定义的属性'contentfulBlog'

1 个答案:

答案 0 :(得分:1)

根据您的gatsby-node.js,并假设您未注释这些行,则“阅读更多”按钮与生成所有t ech.js内容的路径之间将不匹配。

首先,要澄清您的部分解决方案:它将永远不会起作用,因为在您的gatsby-node.js中,您告诉盖茨比,所有contentfulBlog的模板都是特定的模板。您正在根据检索到的数据生成每个页面,并在该模板中传递id(以进行过滤)。由于您已将模板路径移至页面文件夹,因此所有内容均未正确传递并成为undefined

/blog/tech未被设置为gatsby-node.js中任何位置的路径。你有:

path: i === 0 ? `/category/tech` : `/category/tech/${i + 1}`,

因此,如果您的指向/blog/path的“阅读更多”链接,则该链接将始终失败,因为可以正确获取该内容,但绝不会设置该路径。

您需要修复那些路径以适合您的要求。您可以将gatsby-node.js path更改为:

path: i === 0 ? `/blog/tech` : `/blog/tech/${i + 1}`,

或更改“阅读更多”按钮。