gpt4 book ai didi

javascript - GatsbyJS 在从其他组件呈现 GraphQL 查询时出现问题

转载 作者:行者123 更新时间:2023-12-05 05:36:51 28 4
gpt4 key购买 nike

我将 Strapi 用作 Headless CMS,并使用 Gatsby + Graphql 构建我的前端。我有一个“ block 渲染器”组件,用于渲染 strapi 中的任何动态区域。

import React from "react"
import { graphql } from "gatsby"
import BlockHero from "./block-hero"
import BlockParagraph from "./block-paragraph"
import BlockSplitFeature from "./block-split-feature"

const componentsMap = {
// STRAPI__COMPONENT_LAYOUT_ELEMENTS_MULTIPLE_CALLOUT: blockMultipleCallout,
STRAPI__COMPONENT_LAYOUT_ELEMENTS_SIMPLE_PARAGRAPH: BlockParagraph,
STRAPI__COMPONENT_LAYOUT_ELEMENTS_SPLIT_FEATURE: BlockSplitFeature,
STRAPI__COMPONENT_MEDIA_ELEMENT_HERO: BlockHero,
// STRAPI__COMPONENT_META_DATA_DEFAULT_SEO: blockSeo
}

const Block = ({ block }) => {
const Component = componentsMap[block.__typename]

if(!Component) {
return null
}

return <Component data={block} />
}

const BlocksRenderer = ({ blocks }) => {
return (
<div>
{blocks.map((block, index) => (
<Block key={`${index}${block.__typename}`} block={block} />
))}
</div>
)
}

export const query = graphql`
fragment Blocks on STRAPI__COMPONENT_LAYOUT_ELEMENTS_CTASTRAPI__COMPONENT_LAYOUT_ELEMENTS_MULTIPLE_CALLOUTSTRAPI__COMPONENT_LAYOUT_ELEMENTS_SIMPLE_PARAGRAPHSTRAPI__COMPONENT_LAYOUT_ELEMENTS_SPLIT_FEATURESTRAPI__COMPONENT_MEDIA_ELEMENT_HEROUnion {
__typename
... on STRAPI__COMPONENT_LAYOUT_ELEMENTS_MULTIPLE_CALLOUT {
id
MultipleCalloutItem {
id
Heading
Description
}
}
... on STRAPI__COMPONENT_LAYOUT_ELEMENTS_SIMPLE_PARAGRAPH {
id
Text
}
... on STRAPI__COMPONENT_LAYOUT_ELEMENTS_SPLIT_FEATURE {
id
Heading
Description
mediaAlignment
Media {
id
mime
localFile {
childImageSharp {
gatsbyImageData
}
}
alternativeText
}
}
... on STRAPI__COMPONENT_MEDIA_ELEMENT_HERO {
id
Heading
Description
Media {
id
mime
alternativeText
localFile {
url
}
alternativeText
}
}
}
`

export default BlocksRenderer

然后我有我的页面布局文件来生成页面布局(旁注,“布局”元素仅用于导航和页脚。一旦我解决了此页面布局文件问题,这将被重写)>

import React from "react"
import { useStaticQuery, graphql } from "gatsby"
import Layout from "../components/layout"
import Seo from "../components/seo"
import BlocksRenderer from "../components/blocks-renderer"

const PageLayout = () => {
const { allStrapiPage } = useStaticQuery(graphql`
query {
allStrapiPage {
edges {
node {
id
Name
Slug
Blocks {
...Blocks
}
}
}
}
}
`)
const { Blocks } = allStrapiPage

return (
<Layout>
<div>{allStrapiPage.id}</div>
<h1>{allStrapiPage.Name}</h1>
<BlocksRenderer blocks={allStrapiPage.Blocks} />
</Layout>
)
}

export default PageLayout

我正在使用 gatsby-node.js 文件动态创建页面。当我尝试访问其中一个动态创建的 slug 时,我在 blocks-renderer 文件中收到一条错误消息,指出无法访问属性“map”,blocks is undefined。有人有什么想法吗?

编辑:添加了提到的附加文件。

下面是 gatsby-config.js 文件:

/**
* Configure your Gatsby site with this file.
*
* See: https://www.gatsbyjs.com/docs/gatsby-config/
*/

require("dotenv").config({
path: `.env.${process.env.NODE_ENV}`,
})

module.exports = {
/* Your site config here */
plugins: [
"gatsby-plugin-gatsby-cloud",
"gatsby-plugin-postcss",
"gatsby-plugin-sass",
"gatsby-plugin-image",
"gatsby-plugin-sharp",
"gatsby-transformer-sharp",
"gatsby-transformer-remark",
{
resolve: "gatsby-source-strapi",
options: {
apiURL: process.env.STRAPI_API_URL || "http://localhost:1337",
accessToken: process.env.STRAPI_TOKEN,
collectionTypes: [
"drink",
"category",
{
singularName: "page",
queryParams: {
populate: {
Blocks: {
populate: "*",
MultipleCalloutItem: {
populate: "*",
},
},
PageMeta: {
populate: "*",
},
ParentPage: {
populate: "*",
},
},
},
},
],
singleTypes: [
{
singularName: "global",
queryParams: {
populate: {
DefaultSeo: {
populate: "*",
},
Favicon: {
populate: "*",
},
},
},
},
{
singularName: "homepage",
queryParams: {
populate: {
Blocks: {
populate: "*",
},
},
},
},
],
queryLimit: 1000,
}
},
],
}

home.js(按预期工作)。

import React from "react"
import { useStaticQuery, graphql } from "gatsby"
import Layout from "../components/layout"
import Seo from "../components/seo"
import BlocksRenderer from "../components/blocks-renderer"

const HomePage = () => {
const { strapiHomepage } = useStaticQuery(graphql`
query {
strapiHomepage {
Blocks {
...Blocks
}
}
}
`)
const { Blocks } = strapiHomepage

// const seo = {
// metaTitle: title,
// metaDescription: title
// }

return (
<Layout>
<BlocksRenderer blocks={Blocks} />
</Layout>
)
}

export default HomePage

这是我用来生成带有 page-layout.js 文件的页面的 gatsby-node.js 文件。请注意,我可以生成页面和内容,减去 Blocks 查询。

const path = require('path')

exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions

const result = await graphql(
`
query {
allStrapiPage {
edges {
node {
Slug
Name
ParentPage {
Slug
}
}
}
}
}
`
)

if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL Query`)
return
}

const pageTemplate = path.resolve(`./src/layouts/page-layout.js`)
result.data.allStrapiPage.edges.forEach(({ node }) => {
const path = node.Slug
createPage({
path,
component: pageTemplate,
context: {
pagePath: path
},
})
})
}

最佳答案

问题出在这里:

<BlocksRenderer blocks={allStrapiPage.Blocks} />

您不能直接访问Blocks,因为您的nodeedges 属性中的一个数组。据我所知,循环是在 BlocksRenderer 中完成的,因此您需要为其提供一组 blocks。在不确切了解您的数据结构和返回内容的情况下很难猜测,但请尝试类似的方法:

<BlocksRenderer blocks={allStrapiPage.edges.node[0].Blocks} />

I have a Home.js file that is using a different property(allStrapiHomepage) and BlockRenderer is working as expected

如果您的 Home.js 查询使用 page query而不是 static query它们可能会在不同的运行时和构建时间中被触发和混合,因此如果另一个不这样做,则一个可能会失败。这使我认为查询可能没问题,但逻辑不正确。您可以通过添加一个简单的条件来轻松检查它:

<BlocksRenderer blocks={allStrapiPage?.Blocks} />

或者:

{allStrapiPage.Blocks && <BlocksRenderer blocks={allStrapiPage.Blocks} />}

关于javascript - GatsbyJS 在从其他组件呈现 GraphQL 查询时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73243120/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com