【发布时间】:2021-08-06 15:11:47
【问题描述】:
我有一个 Strapi nextjs 应用程序,其中包含 2 类产品。 在本地一切都很好,但是当我尝试构建时出现此错误:
序列化从
getStaticProps返回的.product时出错 “/打印/[名称]”。原因:undefined无法序列化为 JSON。 请使用null或忽略此值。
我不明白的是它只涉及一种产品,而代码是相同的。 我知道这不是最好的配置,但我不能做其他方式。
index.js(书籍)
const HomePage = ({ products }) => {
return (
<div>
<Head>
<title>Catalogue</title>
<meta
name="description"
content="Classe moyenne éditions publie des livres et multiples d'artistes, émergeants ou reconnus, en France et à l'international."
/>
<meta
name="keywords"
content="Edition d'artiste, Livres, prints, multiples, art books, librairie, concept store, Bookshop, Bookstore"
/>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</Head>
<Catalogue products={products} />
</div>
);
};
export async function getStaticProps() {
const products = await getProducts();
return { props: { products } };
}
export default HomePage;
index.js(打印)
const CataloguePage = ({ products }) => {
return (
<div>
<Head>
<title>Catalogue Prints</title>
<meta
name="description"
content="Classe moyenne éditions publie des livres et multiples d'artistes, émergeants ou reconnus, en France et à l'international."
/>
<meta
name="keywords"
content="Edition d'artiste, Livres, prints, multiples, art books, librairie, concept store, Bookshop, Bookstore"
/>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</Head>
<CataloguePrints products={products} />
</div>
);
};
export async function getStaticProps() {
const products = await getProductsPrints();
return { props: { products } };
}
export default CataloguePage;
[名称].js
export default ProductPage;
export async function getStaticProps({ params }) {
const product = await getProductPrints(params.name);
return { props: { product } };
}
// This function gets called at build time
export async function getStaticPaths() {
// Call an external API endpoint to get products
const products = await getProductsPrints();
// Get the paths we want to pre-render based on posts
const paths = products.map(
(product) => `/prints/${product.Name}`
);
// We'll pre-render only these paths at build time.
// { fallback: false } means other routes should 404.
return { paths, fallback: false };
}
如果您也有一些建议可以避免重复几乎相同的代码两次,我很感兴趣。原因是 2 个类别有不同的 json 设计
【问题讨论】:
标签: javascript reactjs next.js strapi encodeuricomponent