gatsbystrapigatsby-plugin

Strapi & Gatsby image issue


I have been following these steps: https://github.com/strapi/gatsby-source-strapi Since image >> publicURL also didn't work, I have reinstalled the newest version gatsby-source-strapi, in order to be able to get publicURL. This goes throug a local file though...

Here is my gatsby-config.js

module.exports = {
  plugins: [
    "gatsby-plugin-react-helmet",
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `images`,
        path: `${__dirname}/src/images`,
      },
    },
    {
      resolve: "gatsby-source-strapi",
      options: {
        apiURL:  "http://localhost:1337",
        contentTypes: ["articles"],
        singleTypes: [`homepage`, `global`],
        queryLimit: 1000,
      },
    },
    "gatsby-plugin-postcss",
  ],
};

My Blog page looks as follows

import React from 'react';
import Footer from '../partials/Footer';
import Navbar from '../partials/Navbar';

import { StaticQuery, graphql } from 'gatsby';

const query = graphql`
  query {
    allStrapiArticles{
      edges {
        node {
          strapiId
          title
          description
          image {
            localFile {
              publicURL
            }
          }
        }
      }
    }
  }
`;

function Blog() {
  return (
    <div className="min-h-screen overflow-hidden">
        <Navbar />
      <div className="max-w-4xl mx-auto py-12 lg:py-16 ">
        <h2 className="text-3xl font-extrabold tracking-tight text-gray-900 sm:text-4xl">
          <span className="block">Coming soon!</span>
          <span className="block text-indigo-600">I am just learning stuff about headless CMS and will build a blog here with strapi.io. Hang in!</span>
        </h2>
      </div>
      <StaticQuery
        query={query}
        render={data => (
          <div className="p-10 grid grid-cols-1 sm:grid-cols-1 md:grid-cols-3 lg:grid-cols-3 xl:grid-cols-3 gap-5">
            {data.allStrapiArticles.edges.map(article => (
              <div className="rounded overflow-hidden shadow-lg">
                <li key={article.node.strapiId}>{article.node.title}</li>
                <img
                  class="w-10 h-10 object-cover object-center rounded-full"
                  src={article.node.image.localFile.publicURL}
                  alt="random user"
                />
              </div>
            ))}
          </div>
        )}
      />
    <Footer />
    </div>
  );
}
export default Blog;

Error: Cannot read property 'publicURL' of undefined. Somehow localfile is undefined... My queries enter image description here


Solution

  • According to:

    The page won't display in dev: Error: Cannot read property 'publicURL' of undefined.

    images is an array so it needs to be accessed as:

    Have you tried?

            {data.allStrapiArticles.edges.map(({ node:article })=> {
             return <div key={article.strapiId} className="rounded overflow-hidden shadow-lg">
                <li key={article.strapiId}>{article.title}</li>
                {article.image[0].localFile && 
                <img
                  class="w-10 h-10 object-cover object-center rounded-full"
                  src={article.image[0].localFile.publicURL}
                  alt="random user"
                />}
              </div>
            })}
    

    Among a destructure and alias in the iterable variable, I've added the key attribute.

    It seems that you have some undefined image somewhere, adding this condition (article.image[0].localFile.publicURL) will print it only if it's available.