Strapi 和 Gatsby 图像问题
Strapi & Gatsby image issue
我一直在遵循这些步骤:https://github.com/strapi/gatsby-source-strapi
由于图像 >> publicURL 也不起作用,我重新安装了最新版本的 gatsby-source-strapi,以便能够获得 publicURL。虽然这是通过本地文件...
这是我的盖茨比-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 未定义......我的查询
根据:
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
) 将仅在可用时打印它。
在 content-types 构建器中选择媒体字段作为单一媒体解决了我的问题
我一直在遵循这些步骤:https://github.com/strapi/gatsby-source-strapi 由于图像 >> publicURL 也不起作用,我重新安装了最新版本的 gatsby-source-strapi,以便能够获得 publicURL。虽然这是通过本地文件...
这是我的盖茨比-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 未定义......我的查询
根据:
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
) 将仅在可用时打印它。
在 content-types 构建器中选择媒体字段作为单一媒体解决了我的问题