【问题标题】:"Cannot read property 'page' of undefined" - Gatsby and Prismic creating pagination for template in gatsby-node“无法读取未定义的属性‘页面’” - Gatsby 和 Prismic 在 gatsby-node 中为模板创建分页
【发布时间】:2020-02-01 21:46:03
【问题描述】:

我希望有人遇到过类似的事情,可以给我一些正确方向的建议。

我正在使用 gatsby 构建一个博客,其中包含从 Prismic 中提取的内容。每篇博文都有一个作者和标签,通过 Prismic 内容关系与之相关。我的目标是通过 gatsby-node 为作者和标签页面动态创建页面,其中还包括相关博客文章的分页。不幸的是,Prismic 似乎并没有建立双向关系,所以我必须通过对我的 allPrismicBlog 过滤作者 uid 进行 graphql 查询来找到相关的博客文章。

我要完成的网址示例: myblog.com/author/作者姓名/ myblog.com/author/author-name/2

我的 gatsby 节点中有以下内容:

exports.createPages = async ({ graphql, actions }) => {
  const { createPage } = actions;
const authors = await graphql(`
    {
      allPrismicAuthor {
        edges {
          node {
            uid
          }
        }
      }
    }
  `);
  authors.data.allPrismicAuthor.edges.forEach(edge => {
    const authorUid = edge.node.uid;
    const authorPosts = graphql(`
    {
      allPrismicBlog(filter: { data: { author: { uid: { eq: ${authorUid} } } } }) {
        edges {
          node {
            uid
        }
      }
    }
    `);
    const numAuthorPages = Math.ceil(authorPosts.length / 2);
    Array.from({ length: numAuthorPages }).forEach((_, i) =>
      createPage({
        path: i === 0 ? `/author/${authorUid}` : `/author/${authorUid}/${i + 1}`,
        component: path.resolve('./src/templates/author.jsx'),
        context: {
          limit: 2,
          skip: i * 2,
          numPages,
          currentPage: i + 1,
          uid: authorUid,
        },
      }),
    );
  });
};

我收到错误TypeError: Cannot read property 'page' of undefined

我不确定我在这里尝试做的是正确的方向,还是我错过了一些重要的事情。任何帮助将不胜感激。

【问题讨论】:

    标签: javascript reactjs gatsby prismic.io


    【解决方案1】:

    想出了一个解决方案,并想在这里分享,以防其他人将来遇到类似的事情。

    我没有尝试使用作者 uid 查询博客文章并处理两个查询的异步性质,而是过滤 blogList 并基于此创建页面。在重构期间可能有几种方法可以改进此代码,但想分享我的工作。

    const blogList = await graphql(`
        {
          allPrismicBlog(sort: { fields: [data___blog_post_date], order: DESC }, limit: 1000) {
            edges {
              node {
                uid
                data {
                  author {
                    uid
                  }
                  tag {
                    uid
                  }
                }
              }
            }
          }
        }
      `);
    
     const posts = blogList.data.allPrismicBlog.edges;
    
    const authors = await graphql(`
        {
          allPrismicAuthor {
            edges {
              node {
                uid
              }
            }
          }
        }
      `);
    
      authors.data.allPrismicAuthor.edges.forEach(edge => {
        const authorUid = edge.node.uid;
    
        const authorBlogs = posts.filter(post => post.node.data.author.uid === authorUid);
        const numAuthorPages = Math.ceil(authorBlogs.length / 1);
    
        for (let i = 0; i <= numAuthorPages; i++) {
          createPage({
            path: i === 0 ? `/author/${authorUid}` : `/author/${authorUid}/${i + 1}`,
            component: pageTemplates.Author,
            context: {
              limit: 1,
              skip: i * 1,
              numPages,
              currentPage: i + 1,
              uid: authorUid,
            },
          });
        }
      });
    

    【讨论】:

      【解决方案2】:

      上面的代码没有显示任何页面变量。

      也许这样可以帮助我查看整个代码?

      也许你忘了事先定义 page 变量

      【讨论】:

      • 当我使用createPage 时,我相信这是来自 gatsby。作为使用 gatsby 模板生成页面的一部分,我不必在任何地方定义 page 变量。例如,以下作品仅创建作者页面而没有任何分页:
      • 执行作者 qraphql 查询,然后遍历结果... authors.data.allPrismicAuthor.edges.forEach(edge => { createPage({ path: /author/${edge.node.uid}, component: pageTemplates.Author , 上下文: { uid: edge.node.uid, }, }); });
      • 您是否尝试过通过输入查询来配置 GraphQL?
      猜你喜欢
      • 2020-01-01
      • 2019-10-15
      • 2019-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 2019-06-08
      • 2021-02-16
      相关资源
      最近更新 更多