Gatsby-plugin-image: Missing image prop after updating Gatsby Client

I'm building a GatsbyJs blog with Contentful and GraphQL queries. Everything was working perfectly until I updated my GatsbyJs client, npm version, and changed the structure of my Contentful Content.

Now my landing page can not load images and in the console, I have the error [gatsby-plugin-image] Missing image prop.

My code:

    import * as React from "react"
import { Link, graphql } from "gatsby"
import { GatsbyImage, getImage } from "gatsby-plugin-image"

import Layout from "../components/layout"
import Seo from "../components/seo"

import {Container, Row, Col, Button} from "react-bootstrap"

import {stars} from "../utilities/starSystem.js"
import "bootstrap/dist/css/bootstrap.min.css";
import 'bootstrap-icons/font/bootstrap-icons.css';
import "../styles/index.css"

class BlogIndex extends React.Component{
  

  render(){

    const { data } = this.props;
    const siteTitle = data.site.siteMetadata.title;
  
      
    return (
      <Layout location={this.props.location} title={siteTitle}>
        <Seo title="Ολά τα άρθρα" />


        <Container>
  
          <Row>
            <Col>
            </Col>
          </Row>
        </Container>

        <Container>
          {data.allContentfulBlogPost.edges.map(post => (
            <section className="book_row">
           <Row>
             <Col xl={6} lg={6} m={3}><div className="eikona"><GatsbyImage className="featured-image" image={getImage(post.node.featuredImage)} alt={post.node.title}/> </div> </Col>
             <Col xl={6} lg={6} m={9}>
               <Button className="custom-button">Non-fiction</Button>
               <h1 className="post-title">{post.node.title}</h1>
               <Row><Col lg={3} className='author-column'><h3 className="author-title">Yuval Harari</h3></Col> 
               <Col> {stars(4)}</Col></Row>
               <p className="perilipsi">{post.node.excerpt.excerpt}</p>
               
              </Col>
           </Row>
           </section>
        ))}
        </Container>
       
  
    </Layout>)
    }
    }

export default BlogIndex;

export const pageQuery = graphql`
  query {
    site {
      siteMetadata {
        title
      }
    }
    allContentfulBlogPost(sort: {order: DESC, fields: publishedDate}) {
      edges {
        node {
          title
          excerpt {
            excerpt
          }
          slug
          publishedDate(formatString: "DD-MM-YY")
          featuredImage {
            gatsbyImageData(width: 230, cropFocus: CENTER, placeholder: BLURRED, resizingBehavior: SCALE)
          }
          body {
            raw
          }
          author
          category
        }
      }
    }
    latest: allContentfulBlogPost(
      sort: {order: DESC, fields: publishedDate}
      limit: 1
    ) {
      edges {
        node {
          title
          featuredImage {
            gatsbyImageData(width: 300)
          }
          publishedDate(formatString: "DD-MM-YY")
          excerpt {
            excerpt
          }
        }
      }
    }
  }
`

When I execute the Query the results seems fine:

{
  "data": {
    "allContentfulBlogPost": {
      "edges": [
        {
          "node": {
            "featuredImage": {
              "gatsbyImageData": {
                "images": {
                  "sources": [
                    {
                      "srcSet": "//images.ctfassets.net/xp88oy0wq1rq/1bgvqRwgZqApVxQeYRCkub/14d2ea0bf162b413bf66c91794e2b34e/sapiens-5.jpg?w=58&h=88&q=50&fm=webp&fit=scale&f=center 58w,\n//images.ctfassets.net/xp88oy0wq1rq/1bgvqRwgZqApVxQeYRCkub/14d2ea0bf162b413bf66c91794e2b34e/sapiens-5.jpg?w=115&h=174&q=50&fm=webp&fit=scale&f=center 115w,\n//images.ctfassets.net/xp88oy0wq1rq/1bgvqRwgZqApVxQeYRCkub/14d2ea0bf162b413bf66c91794e2b34e/sapiens-5.jpg?w=230&h=347&q=50&fm=webp&fit=scale&f=center 230w",
                      "sizes": "(min-width: 230px) 230px, 100vw",
                      "type": "image/webp"
                    }
                  ],
                  "fallback": {
                    "src": "//images.ctfassets.net/xp88oy0wq1rq/1bgvqRwgZqApVxQeYRCkub/14d2ea0bf162b413bf66c91794e2b34e/sapiens-5.jpg?w=230&h=347&fl=progressive&q=50&fm=jpg&fit=scale&f=center",
                    "srcSet": "//images.ctfassets.net/xp88oy0wq1rq/1bgvqRwgZqApVxQeYRCkub/14d2ea0bf162b413bf66c91794e2b34e/sapiens-5.jpg?w=58&h=88&fl=progressive&q=50&fm=jpg&fit=scale&f=center 58w,\n//images.ctfassets.net/xp88oy0wq1rq/1bgvqRwgZqApVxQeYRCkub/14d2ea0bf162b413bf66c91794e2b34e/sapiens-5.jpg?w=115&h=174&fl=progressive&q=50&fm=jpg&fit=scale&f=center 115w,\n//images.ctfassets.net/xp88oy0wq1rq/1bgvqRwgZqApVxQeYRCkub/14d2ea0bf162b413bf66c91794e2b34e/sapiens-5.jpg?w=230&h=347&fl=progressive&q=50&fm=jpg&fit=scale&f=center 230w",
                    "sizes": "(min-width: 230px) 230px, 100vw"
                  }
                },
                "layout": "constrained",
                "width": 230,
                "height": 347.2804532577903,
                "placeholder": {
                  "fallback": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAlgCWAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCAAeABQDASIAAhEBAxEB/8QAGQAAAgMBAAAAAAAAAAAAAAAAAAQDBQYJ/8QAKBAAAgIBBAEDAwUAAAAAAAAAAQIDBBEABRIhMRNBUQYUYRVCgZGx/8QAFQEBAQAAAAAAAAAAAAAAAAAABAL/xAAfEQEAAgEDBQAAAAAAAAAAAAABABECAxJRBCFBYaH/2gAMAwEAAhEDEQA/AOk96nYnWNqzIp4gHm7gfPQUjSMNHcpJBzVIl6JJsSn39gG1Bb3T6fjv3Y7vGKeAIZTKCA+YywIHuOKnvGMqR2R1TbJv+zS/qkm6Q06LVbBWJo52kLQDAjdh5QnkQVP5/OhbjmOOn1mqwe/p8zY2IxGUUZIVQMk5P9nzo0tWkpyV1+xZXrqWQFSSOQYhuz+c6NVDojTFN92rZrMbRz0KU1myojkZvTSUIwIZ8t30vLx5wdK1a1W3Jwn+mtvhWYATSFoXHEeM4GT4GOv80vvNJt1uLP6G32EwFC3avNlUL12GH7i3Xwx0muz2ESRY62zRh1ZfTFJuDZdGyw599Iox14HwNRRxF456u0DP6zWpFViijNJIUrOvqJ6AARs98hjo5850ar6+4fb1oYpwDIgIxCnFAuTxAGfYYH8aNXCt23P/2Q=="
                }
              }
            }
          }
        }
      ]
    }
  }
}

Any help or suggestion would be highly appreciated!

1 answer

  • answered 2021-10-25 07:23 Ferran Buireu

    Maybe is something wrong with ".getImage"

    Definetely. getImage is a helper function that returns undefined if there's a null value (null-safe):

    Safely get a gatsbyImageData object. It accepts several different sorts of objects, and is null-safe, returning undefined if the object passed, or any intermediate children are undefined.

    I think you only need to provide the gatsbyImageData rather than the featuredImage node so your final code should look like:

    getImage(post.node.featuredImage.gatsbyImageData)
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum