本文介绍了Strapi&盖茨比图片问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在执行以下步骤:

解决方案

根据:

images 是一个数组,因此需要按以下方式访问:

您尝试过吗?

  {data.allStrapiArticles.edges.map(({node:article})=> {return< div key = {article.strapiId} className ="四舍五入的溢出隐藏的阴影-lg">< li key = {article.strapiId}> {article.title}</li>{article.image [0] .localFile&< imgclass ="w-10 h-10 object-cover object-center rounded-full"src = {article.image [0] .localFile.publicURL}alt =随机用户"/>}</div>})} 

在可迭代变量的分解结构和别名中,我添加了 key 属性.

似乎您的某个地方有一些 undefined 图片,添加此条件( article.image [0] .localFile.publicURL )仅在可用时才会打印.

I have been following these steps: https://github.com/strapi/gatsby-source-strapiSince 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

解决方案

According to:

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.

这篇关于Strapi&amp;盖茨比图片问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 20:53