[英]Uncaught TypeError: Cannot read properties of undefined (reading 'companyName') JS Objects
i have a problem about when i trying mapping my data.我在尝试映射数据时遇到问题。 I want a reach company name in supplier in products.
我想要在产品供应商中达到公司名称。 How can i fix it?
我该如何解决?
{products.map((repo) => (
<div
style={{
backgroundColor: "#c1d3d4",
marginTop: 50,
display: "flex",
flexDirection: "column",
minWidth: 1000,
paddingLeft: 50,
marginLeft: 400,
paddingRight: 30,
paddingBottom: 12,
borderRadius: 15,
}}
span={24}
>
<p style={{ flex: 1, fontWeight: "bold", fontSize: 26 }}>
{repo.name}
</p>
<p style={{ fontWeight: "bold", fontSize: 14 }}>{repo.supplier.companyName}</p>
<p style={{ fontWeight: "bold", fontSize: 14 }}>
{repo.quantityPerUnit}
</p>
<div
style={{
display: "flex",
flexDirection: "row",
justifyContent: "space-between",
alignContent: "flex-end",
}}
>
<p
style={{
fontSize: 20,
fontWeight: "800",
color: "green",
alignSelf: "flex-end",
}}
>
{repo.unitPrice.toFixed(2)}
</p>
<Button type="primary" onClick={() => AddCart(repo)}>
Sepete Ekle
</Button>
</div>
</div>
))}
this is error message error这是错误信息错误
this is data:这是数据:
https://northwind.vercel.app/api/products https://northwind.vercel.app/api/products
edit:编辑:
@Tim Roberts found the solution. @Tim Roberts 找到了解决方案。 Only some elements have a supplier so other one's dont have.
只有一些元素有供应商,而其他元素没有。 i took the error message when i try map.
当我尝试使用地图时,我收到了错误消息。 I understand now.
我现在知道了。
I think the error occurs because in some data from the api, the supplier prop doesn't exist.我认为发生错误是因为在来自 api 的某些数据中,供应商道具不存在。
Your code你的代码
<p style={{ fontWeight: "bold", fontSize: 14 }}>{repo.supplier.companyName}</p>
Possible Solution可能的解决方案
<p style={{ fontWeight: "bold", fontSize: 14 }}>{repo.supplier && repo.supplier.companyName}</p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.