简体   繁体   English

未捕获的类型错误:无法读取未定义的属性(读取“公司名称”)JS 对象

[英]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.

相关问题 Mocha js Uncaught TypeError:无法读取未定义的属性(读取“应该”) - Mocha js Uncaught TypeError: Cannot read properties of undefined (reading 'should') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“8”) - Uncaught TypeError: Cannot read properties of undefined (reading '8') 未捕获的类型错误:无法读取未定义的属性(读取“”) - Uncaught TypeError: Cannot read properties of undefined (reading '') 未捕获的类型错误:无法读取 null 的属性(正在读取“切片”)------ 未捕获的类型错误:无法读取未定义的属性(正在读取“过滤器”) - Uncaught TypeError: Cannot read properties of null (reading 'slice') ------ Uncaught TypeError: Cannot read properties of undefined (reading 'filter') Uncaught (in promise) TypeError: Cannot read properties of undefined (reading &#39;0&#39;) -- 当所有对象都不是未定义时 - Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '0') -- when all objects are not-undefined Uncaught TypeError TypeError:无法读取未定义的属性(读取“路径”) - Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'path') 未捕获的类型错误:无法读取未定义的属性(读取“未定义”) - Uncaught TypeError: Cannot read properties of undefined (reading 'undefined') 未捕获的类型错误:无法读取未定义的属性(读取“目标”)和(读取“值”) - Uncaught TypeError: Cannot read properties of undefined (reading 'target') & (reading 'value')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM