Skip to content
Advertisement

Gatsby: getImage returns undefined

getImage is returning undefined so my GatsbyImage component is not rendered.

File structure:

  • src/pages/gallery.js
  • src/images (has 12 photos named photo-01.jpg, photo-02.jpg, …)

I have the following code (gallery.js):

JavaScript

what can i have wrong?

Advertisement

Answer

The problem is that you are mixing gatsby-image (from Gatsby 1 to Gatsby 2) and gatsby-plugin-image (from Gatsby 3 onwards). The first one is now deprecated.

This can be easily spotted when you query for fluid or fixed GraphQL nodes since they are created by gatsby-image which uses Img (from 'gatsby-image‘) component, which accepts a fluid or fixed property.

On the other hand, gatsby-plugin-image queries for gatsbyImageData (not fluid or fixed) and the corresponding GatsbyImage (from 'gatsby-plugin-image') component accepts an image property.

In your case, you are querying a gatsby-image structure applied in a gatsby-plugin-image component, that’s why it is returning undefined.

Check the migration guide but essentially you will need to replace (and remove) all references to gatsby-image and install the required dependencies:

JavaScript

Add it to your gatsby-config.js:

JavaScript

And change your query to something like:

JavaScript

Double-check the query since the nodes and the structure may be different when applying gatsby-plugin-image.

Note that getImage is a helper function and you may not need it, consider using directly:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement