【发布时间】:2021-04-09 13:31:09
【问题描述】:
我有一个非常简单的 Gatsby 应用程序。我正在运行 gatsby develop 并且它工作正常但是当我运行 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,
},
})
})
}
【问题讨论】:
-
你能分享你的
gatsby-node.js吗? -
用 gatsby-node.js 更新帖子
标签: node.js build graphql gatsby production