2

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!

Costis94
  • 250
  • 2
  • 14
  • Have you checked that in all your posts there's a valid image? – Ferran Buireu Oct 25 '21 at 04:49
  • Hi Ferran! I tried the same with a single post, which haves valid data but still not working. I installed React Dev tools and I find that the missing prop is "image:undefined", but when I execute the query all the data seems valid. Maybe is something wrong with ".getImage" ??? – Costis94 Oct 25 '21 at 07:17

1 Answers1

2

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)
Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67
  • Nope, exactly the same results! – Costis94 Oct 25 '21 at 07:51
  • 2
    Hmm... because it's a helper function, you don't need to use it. Try directly: `image={post.node.featuredImage.gatsbyImage}` – Ferran Buireu Oct 25 '21 at 07:52
  • 2
    I actually used {post.node.featuredImage.gatsbyImageData} and now everythink is working again! You are the hero of the day! Thank you very much for your time, I'm searching this for two days now! Please update your answer and I will accept it! – Costis94 Oct 25 '21 at 07:58