graphql - Nextjs - 内容丰富 - 标签 - 动态页面
问题描述
当我点击文章或我网站上其他地方的标签时,我想创建动态页面。
我正在使用 Next.js、SSG,并使用以下 GraphQL 查询从 Contentful 中获取包含标签的文章:
export async function getArticles() {
const articlesQuery = gql`
{
articleCollection(order: date_DESC) {
items {
title
slug
excerpt
date
contentfulMetadata {
tags {
name
id
}
}
featuredImage {
title
url
width
height
}
author {
name
photo {
fileName
url
width
height
}
title
twitterProfile
linkedInProfile
slug
}
}
}
}
`;
return graphQLClient.request(articlesQuery);
}
export async function getArticle(slug) {
const articleQuery = gql`
query getArticle($slug: String!) {
articleCollection(limit: 1, where: { slug: $slug }) {
items {
title
slug
excerpt
date
contentfulMetadata {
tags {
name
id
}
}
featuredImage {
title
url
width
height
}
author {
name
photo {
fileName
url
width
height
}
title
twitterProfile
linkedInProfile
slug
}
content {
json
links {
entries {
block {
sys {
id
}
__typename
... on VideoEmbed {
title
embedUrl
}
... on CodeBlock {
description
language
code
}
}
}
assets {
block {
sys {
id
}
url
title
width
height
}
}
}
}
}
}
}
`;
return graphQLClient.request(articleQuery, {
slug,
});
}
contentfulMetadata 是标签的来源:
contentfulMetadata {
tags {
name
id
}
}
这是我的 [id].jsx 文件:
import { getArticles, getArticle } from "@utils/contentful";
export async function getStaticPaths() {
const data = await getArticles();
return {
paths: data.articleCollection.items.map((article) => ({
params: { id: article.contentfulMetadata.tags[0].id },
})),
fallback: false,
};
}
export async function getStaticProps(context) {
const data = await getArticle(context.params.id);
return {
props: { article: data.articleCollection.items[0] },
};
}
export default function TagPage({ article }) {
return (
<div>
<h1>{article.contentfulMetadata.tags.id}</h1>
</div>
);
}
我收到以下错误:
Error: Error serializing `.article` returned from `getStaticProps` in "/tags/[id]". Reason: `undefined` cannot be serialized as JSON. Please use `null` or omit this value.
当console.log(data.articleCollection.items.contentfulMetadata.tags.id);
或console.log(data.articleCollection.items.contentfulMetadata.tags[0].id);
在 getStaticPaths 函数中时,它会提供以下错误:
TypeError: Cannot read property 'tags' of undefined
谁能展示如何创建一个动态页面 ([id].jsx) 文件,该文件将标签 id 显示为标题<h1>
以及包含相同标签的所有文章?
解决方案
内容丰富的 DevRel 在这里。
article.contentfulMetadata.tags
是一个数组,因为一个条目可以有多个标签。因此,您需要通过article.contentfulMetadata.tags[0].id
orarticle.contentfulMetadata.tags[desired_index].id
等方式访问您想要的标签。
这是一个示例 GraphQL 查询:
query {
blogPostCollection {
items {
contentfulMetadata {
tags {
id
name
}
}
}
}
}
这是带有标签作为数组的响应:
"data": {
"blogPostCollection": {
"items": [
{
"contentfulMetadata": {
"tags": [
{
"id": "salmastag",
"name": "Salma s tag"
}
]
}
},
{
"contentfulMetadata": {
"tags": []
}
}
]
}
}
}
请注意,如果博客文章没有分配任何 PUBLIC 标记(响应中的第二个条目),则会返回一个空数组——您可能需要为此在代码中进行一些安全检查。
推荐阅读
- python - 绘制水平数据并清楚地看到它的最佳方法?
- floating-point - IEEE-754 浮点标准:表示数字
- javascript - 在Javascript中将int数组转换为字节数组
- python - 如何使文本摘要模型预测单个输入?
- seqhmm - em.depmix 中的错误(object = object,maxit = emcontrol$maxit,tol = emcontrol$tol,:起始值不可行;请提供它们
- node.js - 奇怪的错误'UnhandledPromiseRejectionWarning:ReferenceError:消息未定义'
- javascript - 将使用 PHP 中的 While 创建的输入中的数量添加到 DIV
- css - CSS 关键帧动画在一个选择器上工作,但在其他选择器上不工作
- javascript - 网格外的JqGrid自定义分页
- node.js - nodeJS:在 utf-8 中转换 response.body(来自 windows-1251 编码)