【发布时间】:2021-09-02 22:50:14
【问题描述】:
我正在向我的 Gatsby 博客添加标签并生成一个页面,该页面将查询带有该标签的所有帖子。该页面正在生成,但帖子未显示。我重新编写了代码,现在出现了这个错误:
Original error fixed. see update below.
这是我的模板文件:
import { Link, graphql } from "gatsby"
import * as React from "react"
import Layout from "../components/layout/layout"
// import PropTypes from "prop-types"
const TagList = ({ pageContext, data }) => {
const title = pageContext
const { totalCount } = data.allMdx.group
return (
<Layout>
<h3>{ data.allMdx.group.fieldValue }</h3>
<ul>
{ data.allMdx.group.map( ({ nodes }) => {
return (
<li key={ nodes.id }>
<Link to={ `/blog/${ nodes.slug}` }>{nodes.title}</Link>
</li>
)
}
)}
</ul>
<Link to="/blog">All tags</Link>
</Layout>
);
}
export default TagList;
export const query = graphql`
query MyQuery($tags: String) {
allMdx(
filter: { frontmatter: { tags: { in: [$tags] } } }
) {
group(field: frontmatter___tags) {
fieldValue
nodes {
id
slug
frontmatter {
title
}
}
}
}
}
`;
这是我的 gatsby-node.js 文件:
const path = require(`path`)
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions;
const tagTemplate = path.resolve('src/templates/tagTemplate.js');
const result = await graphql(`
query Tags {
allMdx {
group(field: frontmatter___tags) {
fieldValue
nodes {
id
slug
frontmatter {
title
}
}
}
}
}
`)
if (result.errors) {
console.log(result.errors);
reporter.panicOnBuild(`Error whil running GraphQL query`);
}
const tags = result.data.allMdx.group
tags.forEach(tags => {
createPage({
path: `/tags/${tags.fieldValue}/`,
component: tagTemplate,
context: {
tags: tags.fieldValue
}
})
});
};
是的,我多次阅读 Gatsby 文档。它没有解决我的问题。谷歌也没有。
更新:
我设法让它部分工作。页面出现了,但没有数据出现。它是未定义的,我不知道为什么。
我更新了上面的代码块以反映我所做的更改。我没有收到错误,否则我会发布。
【问题讨论】:
-
你能提供一个Codesandbox吗?