我尝试在json文件中循环遍历产品,但一直收到此错误TypeError:www.example.com不是函数products.map is not a function**
这是我代码
import React from "react"; import Link from "next/link";
function shop({ products }) { return (
<div>
<nav>
<Link href="/">Home</Link>
<Link href="/shop">Shop</Link>
</nav>
<div className="title1">
<h1>Explore All Products</h1>
</div>
<>
{products.map((product) => { return (
<div key={product.id}>
<Link href="">
<h2>
{product.id} {product.title}
</h2>
</Link>
</div>
);
})}
</>
</div> ); }
export default shop;
export async function getStaticProps() { const response = await fetch("https://dummyjson.com/products"); const data = await response.json(); console.log(data);
return { props: { products: data,
}, }; }
我尝试了很多其他人对堆栈溢出的东西,没有任何工作
2条答案
按热度按时间4ktjp1zp1#
vq8itlhq2#
端点
https://dummyjson.com/products
返回如下对象:所需的数据位于
products
属性中,因此只需用途: