Using gatsby-plugin-image causes error: can't access property "image" #34164
-
I have a blog template that I would like to display a single image from the blog post beside the frontmatter (slug, date, title, excerpt). However, I keep getting this error:
Here is my
If I remove the GatsbyImage code then the page loads fine... I am following the official documentation, which can be found here Any pointer/guidance would be greatly appreciated. Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
You probably missed one thing in your code. You should source your images in the post loop — in
something like this : // ...
{
data.allMdx.nodes.map((node, key) => {
const {frontmatter} = node
const image = getImage(frontmatter.image)
return (
<div className="flex justify-center align-middle" key={key}>
{/* ... */}
<GatsbyImage image={image} alt={frontmatter.imageAlt} />
{/* ... */}
</div>
)}
} |
Beta Was this translation helpful? Give feedback.
-
That was it! Thank you @violy very very much 🤩 🥳. My updated code now looks like this:
|
Beta Was this translation helpful? Give feedback.
-
As an extra perk it also cleared up my Netlify Build errors!
|
Beta Was this translation helpful? Give feedback.
You probably missed one thing in your code.
You try to source an image in a bad scope on a
undefined
object.You should source your images in the post loop — in
map()
function scopesomething like this :