我有一个非常简单的盖茨比应用程序。我正在运行gatsby Development,它工作良好,但当我运行gatsby build时,我得到了一个错误。我不知道为什么开发能工作,而构建不能。不知道我错过了什么!
import React from "react"
import Layout from "./layout"
const ProductTemplate = ({ pageContext }) => {
const { product } = pageContext
return (
<Layout>
<h1>{product.title}</h1>
<div>{product.description}</div>
</Layout>
)
}
export default ProductTemplate
这是确切的错误
8 | return (
9 | <Layout>
> 10 | <h1>{product.title}</h1>
| ^
11 | <div>{product.description}</div>
12 |
WebpackError: TypeError: Cannot read property 'title' of undefined
- product.js:10
src/pages/product.js:10:20
gatsby-node.js
const path = require(`path`)
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
// Query for all products in Shopify
const result = await graphql(`
query {
allShopifyProduct(sort: { fields: [title] }) {
edges {
node {
title
shopifyId
handle
description
availableForSale
priceRange {
maxVariantPrice {
amount
}
minVariantPrice {
amount
}
}
}
}
}
}
`)
// Iterate over all products and create a new page using a template
// The product "handle" is generated automatically by Shopify
result.data.allShopifyProduct.edges.forEach(({ node }) => {
createPage({
path: `/product/${node.handle}`,
component: path.resolve(`./src/pages/product.js`),
context: {
product: node,
},
})
})
}
您的 这只在运行 要修复它,只需将 对此: 这是因为您正在为每个产品创建一个模板,而不是一个页面(就像盖茨比理解一个页面一样)。 如果您想要了解完整的解释,下面是一个GitHub线程,其中包含您的问题。文件夹中(应该位于
文件夹中的已知页面。
并更改以下内容:
component: path.resolve(`./src/pages/product.js`),
component: path.resolve(`./src/templates/product.js`),