Gatsby 盖茨比·佩奇不';t从数据以编程方式创建

Gatsby 盖茨比·佩奇不';t从数据以编程方式创建,gatsby,Gatsby,我正在尝试使用盖茨比(v2.18.17)为我的博客生成页面。我已经在Gatsby网站上实现了教程中的代码示例,我在那里使用CreatePagesAPI。页面不会生成,并且没有警告或错误 gatsby node.js const path=require(`path`) const{createFilePath}=require(`gatsby源文件系统`) exports.onCreateNode=({node,getNode,actions})=>{ const{createNodeField

我正在尝试使用盖茨比(v2.18.17)为我的博客生成页面。我已经在Gatsby网站上实现了教程中的代码示例,我在那里使用CreatePagesAPI。页面不会生成,并且没有警告或错误

gatsby node.js

const path=require(`path`)
const{createFilePath}=require(`gatsby源文件系统`)
exports.onCreateNode=({node,getNode,actions})=>{
const{createNodeField}=操作
if(node.internal.type==`MarkdownRemark`){
const slug=createFilePath({node,getNode,basePath:`pages`})
createNodeField({
节点,
名称:`slug`,
值:slug,
})
}
}
exports.createPages=异步({graphql,actions})=>{
const{createPage}=actions
常量结果=等待图形ql(`
质疑{
所有的标记{
边缘{
节点{
田地{
鼻涕虫
}
}
}
}
}
`)
result.data.allMarkdownRemark.edges.forEach(({node})=>{
创建页面({
路径:node.fields.slug,
组件:path.resolve(`./src/templates/post.js`),
背景:{
//传递到上下文的数据可用
//作为GraphQL变量的页内查询。
slug:node.fields.slug,
},
})
})
}
src/templates/post.js

从“React”导入React
从“./组件/布局”导入布局
导出默认值()=>{
返回(
你好,博文
)
}
构建输出仅生成11页(其中8页是手动创建的-我不确定其他3页是什么)

content/posts

> gatsby develop

success open and validate gatsby-configs - 0.034s
success load plugins - 0.578s
success onPreInit - 0.002s
success initialize cache - 0.007s
success copy gatsby files - 0.071s
success onPreBootstrap - 0.008s
success createSchemaCustomization - 0.009s
success source and transform nodes - 0.233s
success building schema - 0.304s
success createPages - 0.101s
success createPagesStatefully - 0.090s
success onPreExtractQueries - 0.001s
success update schema - 0.033s
success extract queries from components - 0.270s
success write out requires - 0.046s
success write out redirect data - 0.002s
success Build manifest and related icons - 0.106s
success onPostBootstrap - 0.112s
⠀
info bootstrap finished - 3.825 s
⠀
success run queries - 0.039s - 12/12 303.82/s
我想知道我的网站是否存在配置问题。但是,当我在遍历标记文件的同时将slug写入控制台时,slug会在构建时打印出来


代码在上可用。

gatsby-plugin-routes
在您的
gatsby-config.js
中以某种方式干扰了
gatsby-node.js
中的createPages函数

这里是我编辑您的项目时工作的
gatsby node.js

exports.createPages = ({ actions, graphql }) => {
  const { createPage } = actions;
  const blogTemplate = path.resolve("src/templates/post.js");
  return graphql(`
{
  allMarkdownRemark(filter: {fileAbsolutePath: {regex: "content/posts/"}}) {
    edges {
      node {
        fields {
          slug
        }
      }
    }
  }
}
  `).then((result) => {
    if (result.errors) {
      return Promise.reject(result.errors);
    }
    /* --- Create blog pages --- */
    result.data.allMarkdownRemark.edges.forEach(({ node }) => {
      console.log(node.fields.slug);
      createPage({
        path: `/blog${node.fields.slug}`,
        component: blogTemplate,
        context: {
          slug: node.fields.slug,
        },
      });
    });
  });
};
您需要从
gatsby config.js
中删除
gatsby插件路由

/*    {
      resolve: `gatsby-plugin-routes`,
      options: {
        path: `${__dirname}/gatsby-routes.js`,
      },
    },*/
我建议在网站的其他部分运行之前不要使用routes插件。每个插件都会增加项目的复杂性,在这种情况下会导致无声的失败


那是一个很好的博客。我将为自己的投资组合带来一些灵感。:)

当我构建你的项目时,我得到了
TypeError:无法读取
C:/Users/User/code/src/components/image.js:6的未定义的属性'node'。更改GraphQL查询或编辑任何gatsby配置文件后,是否重新启动了
gatsby develop
?这可能就是你没有看到错误的原因。谢谢你的帮助。这个错误很奇怪。我替换了
以返回一个
,同样,没有错误,也没有生成页面。如果同时更改
图像
组件,会发生什么情况?页面生成了吗?是的,现在对我有用了。我将
返回渲染(图像)
替换为
返回。你的项目为我创建了10页。如果我计算正确,它需要创建100多页。由于某些原因,无法生成博客页面。我想知道为什么。我在玩不同版本的盖茨比,看看我是否得到了我需要的。有什么想法吗?谢谢感谢你的赞美和帮助。我会很快试一试,让你知道。传奇!