我正在使用 astro 和 React 进行动态路由,当进行获取以获取对象数组时,它告诉我 Product.title 属性永远不会,我不知道为什么
src/pages/category/[类别].astro
---
import type { Product } from "../../types/component.type";
import Layout from "../../layouts/Layout.astro";
export async function getStaticPaths(category: Product) {
const categoryToSearch = category.id
const data = await fetch(https://api.escuelajs.co/api/v1/categories/${categoryToSearch}/products).then(response => response.json());
return data.map((category: Product) => {
return {
params: { id: category.id },
props: { products: data },
};
});
}
const { id } = Astro.params;
const { products } = Astro.props;
---
<Layout title={id}>
<Layout title={id}>
<section class="py-36">
{products.map((product: Product) => (
<h1>{product.title}</h1>
))}
</section>
</Layout>
</Layout>
我收到此错误:
14:18:16 [WARN] [router] getStaticPaths() ignored in dynamic page /src/pages/category/[category].astro. Add export const prerender = true; to prerender the page as static HTML during the build process.
14:18:17 [200] /category/1 16ms
14:18:17 [ERROR] Cannot read properties of undefined (reading 'map')
Stack trace:
at Object.default (C:\Users\facum\modern-ecommerce\src\pages\category\[category].astro:27:17)
[...] See full stack trace in the browser, or rerun with --verbose.
我必须返回一个对象数组,以便稍后能够为我正在做的电子商务组合产品的动态路线
您在获取的字符串周围缺少引号。应该是:
const data = await fetch("https://api.escuelajs.co/api/v1/categories/${categoryToSearch}/products")
.then(response => response.json());