我得到了这个错误,甚至我的查询在GraphQL资源管理器中给了我一个正确的结果。
我试图加载文章的封面图片,但我看不出哪里是错误。
有很多博客文章和同样的问题,但我没有找到正确的答案,以纠正这个错误。
错误: TypeError:无法读取空的属性“childImageSharp”
这就是我迄今为止尝试过的:
Mdx
---
title: Hello World - from mdx!
date: 2019-06-01
published: true
tags: ['react', 'javascript']
---盖茨比组件
const Blog = ({ data }) => {
const tags = data.allMdx.group
const posts = data.allMdx.nodes
return (
<Layout>
<div className="hero blog-section">
<div className="hero-body">
<div className="container">
<h1 className="home-title is-size-1">Blog</h1>
<h3 className="home-subtitle is-size-4">
Thoughts about programming, design and random stuff
</h3>
<div className="tags"></div>
<div class="tags">
{tags.map(tag => (
<Link
to={`/tags/${kebabCase(tag.fieldValue)}/`}
className="tag is light"
>
{tag.fieldValue}
</Link>
))}
</div>
{posts.map(({ id, excerpt, frontmatter, fields }) => (
<Link to={fields.slug}>
<div key={id} className="card is-fullimage mb-1">
<Img fluid={frontmatter.cover.childImageSharp.fluid} />
<div className="card-stacked">
<div className="card-content">
<time className="home-red">{frontmatter.date}</time>
<h1 className="is-size-4">{frontmatter.title}</h1>
<p>{excerpt}</p>
<span className="home-red">
{fields.readingTime.text}
</span>
</div>
</div>
</div>
</Link>
))}
</div>
</div>
</div>
</Layout>
)
}查询
export const query = graphql`
query SITE_INDEX_QUERY {
allMdx(
sort: { fields: [frontmatter___date], order: DESC }
filter: { frontmatter: { published: { eq: true } } }
) {
group(field: frontmatter___tags) {
fieldValue
}
nodes {
id
excerpt(pruneLength: 100)
frontmatter {
tags
title
date(formatString: "MM/DD/YYYY")
cover {
publicURL
childImageSharp {
fluid(maxWidth: 2000, traceSVG: { color: "#639" }) {
tracedSVG
}
}
}
}
fields {
slug
readingTime {
text
}
}
}
}
}
`gatsby-config.js
module.exports = {
siteMetadata: {
title: `Lorem Ipsum Blog`,
description: `Lorem ipsum.`,
},
plugins: [
{
resolve: `gatsby-source-filesystem`,
options: {
path: `${__dirname}/src/images`,
name: `images`,
},
},
{
resolve: `gatsby-source-filesystem`,
options: {
path: `${__dirname}/posts`,
name: `posts`,
},
},
{
resolve: `gatsby-plugin-mdx`,
options: {
extensions: [`.mdx`, `.md`],
},
},
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
{
resolve: `gatsby-remark-images`,
options: {
maxWidth: 630,
},
},
],
},
},
`gatsby-transformer-sharp`,
`gatsby-plugin-sharp`,
`gatsby-plugin-sass`,
`gatsby-plugin-dark-mode`,
`gatsby-plugin-feed`,
`gatsby-remark-reading-time`,
],
}提前感谢
发布于 2021-01-03 18:16:22
根据你的MDX:
---
title: Hello World - from mdx!
date: 2019-06-01
published: true
tags: ['react', 'javascript']
---并不是所有的帖子都有封面图像,所以现在您正在请求frontmatter.cover.childImageSharp.fluid,您的代码正在中断。在以下位置添加一个简单的条件:
const tags = data.allMdx.group
const posts = data.allMdx.nodes
return (
<Layout>
<div className="hero blog-section">
<div className="hero-body">
<div className="container">
<h1 className="home-title is-size-1">Blog</h1>
<h3 className="home-subtitle is-size-4">
Thoughts about programming, design and random stuff
</h3>
<div className="tags"></div>
<div class="tags">
{tags.map(tag => (
<Link
to={`/tags/${kebabCase(tag.fieldValue)}/`}
className="tag is light"
>
{tag.fieldValue}
</Link>
))}
</div>
{posts.map(({ id, excerpt, frontmatter, fields }) => (
<Link to={fields.slug}>
<div key={id} className="card is-fullimage mb-1">
{frontmatter.cover && <Img fluid={frontmatter.cover.childImageSharp.fluid} /> }
<div className="card-stacked">
<div className="card-content">
<time className="home-red">{frontmatter.date}</time>
<h1 className="is-size-4">{frontmatter.title}</h1>
<p>{excerpt}</p>
<span className="home-red">
{fields.readingTime.text}
</span>
</div>
</div>
</div>
</Link>
))}
</div>
</div>
</div>
</Layout>
)
}基本上,如果frontmatter.cover不附带环形的帖子,就不要打印图像。
{frontmatter.cover && <Img fluid={frontmatter.cover.childImageSharp.fluid} /> }如果您已经从新的可选链接的Babel插件中添加了ECMAScript,则可以通过以下方式简化它:
<Img fluid={frontmatter?.cover?.childImageSharp?.fluid} />请记住,同时使用gatsby clean清理缓存。
发布于 2021-01-03 20:47:26
奇怪的答案,但使用gatsby clean,我设法解决了这个问题。
https://stackoverflow.com/questions/65553158
复制相似问题