gpt4 book ai didi

gatsby - Strapi & Gatsby 图像问题

转载 作者:行者123 更新时间:2023-12-04 17:19:09 30 4
gpt4 key购买 nike

我一直在遵循以下步骤:https://github.com/strapi/gatsby-source-strapi
由于 image >> publicURL 也不起作用,我重新安装了最新版本的 gatsby-source-strapi,以便能够获得 publicURL。这虽然通过本地文件...
这是我的 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",
],
};
我的博客页面如下所示
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;
错误:无法读取未定义的属性“publicURL”。不知何故 localfile 未定义......我的疑问
enter image description here

最佳答案

根据:

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

images是一个数组,因此需要按以下方式访问它:
你有没有尝试过?
        {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>
})}
在可迭代变量的解构和别名中,我添加了 key属性。
看来你有一些 undefined某处的图像,添加此条件( article.image[0].localFile.publicURL )只会在它可用时打印它。

关于gatsby - Strapi & Gatsby 图像问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67270962/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com