【发布时间】:2021-05-28 09:18:18
【问题描述】:
我最近完成了 Tom Phillips 在 Udemy (https://www.udemy.com/share/101XRSAEEedF5aQH4D/) 上的一门课程,并且我能够完成该课程并且它运行良好,直到我开始为我自己的个人作品集修改代码。我在 Digital Ocean 上部署了一个 WP 后端,我的前端托管在 Netlify 上。
我遇到的具体问题似乎是没有为“投资组合”的自定义帖子类型返回数据部署日志指出以下内容:
> failed Building static HTML for pages - 5.032s
> error Building static HTML failed for path "/portfolio/"
>
> 58 | style={{ margin: "1rem", maxWidth: "345" }}
> 59 |
> 60 | {console.log(portfolioItem.node.featured_media.source_url)}
> |
> 61 | <ImageWrapper>
> 62 | <FeaturedImage>
> 63 | <CardMedia
>
> WebpackError: TypeError: Cannot read property 'source_url' of null
>
> - PortfolioItems.js:60
> src/components/portfolio/PortfolioItems.js:60:66
>
> - PortfolioItems.js:54
> src/components/portfolio/PortfolioItems.js:54:50
>
> ────────────────────────────────────────────────────────────────
> "build.command" failed
> ────────────────────────────────────────────────────────────────
>
> Error message
> Command failed with exit code 1: gatsby build
>
> Error location
> In Build command from Netlify app:
> gatsby build
> Resolved config
> build:
> command: gatsby build
> commandOrigin: ui
> environment:
> - API_PROTOCOL
> - API_URL
> - NODE_VERSION
> publish: /opt/build/repo/public>
StaticQuery 看起来像:
<StaticQuery
query={graphql`
{
allWordpressWpPortfolio {
edges {
node {
excerpt
content
title
slug
featured_media {
source_url
}
}
}
}
}
`}
render={props => (
<PortfolioItemsWrapper>
<Grid
container
// direction="column"
justify="center"
alignItems="center"
>
{props.allWordpressWpPortfolio.edges.map(portfolioItem => (
<Grid item xs={12} md={6}>
<Card
key={portfolioItem.node.id}
style={{ margin: "1rem", maxWidth: "345" }}
>
{console.log(portfolioItem.node.featured_media.source_url)}
<ImageWrapper>
<FeaturedImage>
<CardMedia
style={{ maxWidth: "100%" }}
image={portfolioItem.node.featured_media.source_url}
title="Thumbnail"
component="img"
/>
</FeaturedImage>
</ImageWrapper>
<h2>{portfolioItem.node.title}</h2>
<div
dangerouslySetInnerHTML={{
__html: portfolioItem.node.excerpt,
}}
/>
<Link to={`/portfolio/${portfolioItem.node.slug}`}>
Read more...
</Link>
</Card>
</Grid>
))}
</Grid>
</PortfolioItemsWrapper>
)}
/>
由于当 WP 站点使用本地应用程序托管在 localhost 上时它在本地工作,我假设 Digital Ocean 上的 WP 配置中有一些东西阻止访问,但我不熟悉 PHP,并且无法找到错误。我的 .htaccess 文件如下所示:
# BEGIN WordPress
php_value upload_max_filesize 128M
php_value post_max_size 128M
php_value memory_limit 256M
php_value max_execution_time 300
php_value max_input_time 300
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
如果您需要 Wordpress 配置中的更多内容,请告诉我。我不确定还有什么与该问题相关。 wp-admin 设置在我的本地 localhost 版本和部署版本中是相同的
【问题讨论】:
-
你能把整个组件放在你使用
staticQuery的地方吗(不是它的一部分,因为它失去了上下文) -
感谢@FerranBuireu 我已经编辑了带有完整 StaticQuery 标签的问题。如前所述,这在本地工作得很好,但不是在部署的站点上,所以我的预感是这是一个 WP 配置错误,而不是一个反应语法错误,但如果这有帮助,请告诉我。再次感谢!
-
用远程 WP 试试本地 gatsby 吗? ...这篇文章/CPT 根本没有特色图片?你没有在反应中测试这种可能性