【问题标题】:TypeError: Cannot read properties of undefined (reading 'customer')TypeError:无法读取未定义的属性(读取“客户”)
【发布时间】:2021-11-21 11:42:57
【问题描述】:
import React, { useState, useEffect } from 'react';
import axios from 'axios';

{const CustomerList = () => {
  const [information, setInformation] = useState([]);
  const [isLoading, setIsLoading] = useState(true);

  useEffect(() => {
    if (isLoading) {
      axios.get('').then((response) => {
        setInformation((prevInfo) => {
          return [...prevInfo, response.data];
        });
      });
    }
    return () => {
      setIsLoading(false);
    };
  }, [information, isLoading]);

  return (
    <>
      <h2>Name: </h2>
      {information.map((item, index) => (
        <div>
          <div>{item[index].customer.firstName}</div>
          <div>{item[index].customer.lastName}</div>
        </div>
      ))}
    </>
  );
};

export default CustomerList;
  1. 我正在使用 axios 和 useEffect 从 api 获取数据
  2. 我通过数据的响应数组进行映射,如果数组中没有信息,则应用程序中断
  3. 如何仅映射数组中特定数量的项目?
  4. 例如,如果我的数据数组中有 3 个项目,它将在浏览器上从返回语句向下显示 但是...当它映射所有 3 个时,它将继续重试并中断,因此“客户未定义”,因为没有更多客户 5.我尝试做 {item.customer.firstName 并得到 undefined first name 的错误}

有什么办法可以解决这个问题?谢谢!

enter image description here

【问题讨论】:

    标签: javascript arrays reactjs api array.prototype.map


    【解决方案1】:

    map 不是这样工作的。将这些参数传递给回调:

    information.map((item, index) => ...
    

    index 是迭代 information 数组时的当前索引,item 是该索引处的元素。它不是整个数组的另一个副本。所以不要这样:

    item[index].customer.firstName
    

    你会这样做:

    item.customer.firstName
    

    顺便说一句,在 React 中迭代数组以输出列表时,您可能希望将 key 添加到顶级元素。如果您愿意,可以使用 index 来实现此目的:

    {information.map((item, index) => (
        <div key={index}>
          ...
        </div>
    ))}
    

    或者,如果 item 具有唯一标识符,那么您可以使用它并完全删除 index

    【讨论】:

    • 当我执行 item.customer.firstName 时,我得到未定义的名字。这可能是因为来自 api 的数据是数组 { [ ], [ ], [ ] } 的对象吗?我有 [index] 的原因是因为我必须定位一个特定的数组然后获取客户
    • @Jcodes:嗯,我们不知道你的数据是什么。但作为调试的一部分,您绝对应该检查该数据,以便您知道它是什么。 information 和任何给定的 item 在渲染时的确切运行时值是多少?
    • 我用数据结构在问题中添加了一张图片
    • @Jcodes: 那么information是一个有2个元素的数组,每个元素都有一个以cus开头的属性。该属性可能被称为customer,并且该客户属性可能是一个对象,并且该对象可能有一个称为firstName的属性, 并且 firstName 属性 可能 有一个值。该图片中不存在任何这些信息。我们鼓励您在调试中找到该信息。
    • 正确,因此信息状态设置为response.data,它指向一个具有两个数组元素{ [0], [1] } 的对象。然后我们映射该对象(问题?)并定位每个数组,然后每个数组都有一个 customer > firstName > value 的对象。如果我在 api 调用中执行response.data[0],我们只会得到一个数组,那么我可以执行{item.customer.firstName},但只能针对一个数组
    【解决方案2】:

    item[index] 不正确,因为item 无论如何都指向index 处的数组元素。

    正确方法:

    const data = [1,2,3,4,5,6,7,8,9,10]
    
    const mappedData = data.map((d, index) => {
        console.log("index is", index)
        console.log("d is", d)
        console.log("-----------------------------")
        return d
    })

    你想做什么:

    const data = [1,2,3,4,5,6,7,8,9,10]
    
    const mappedData = data.map((d, index) => {
      console.log("index is", index)
      console.log("d[index] is", d[index]) //undefined since d is not an array 
      console.log("------------------------------")
      return d
    })

    【讨论】:

      【解决方案3】:

      当您map() 一组项目时,您将分别处理每个item,因此您不需要index,您可以将index 用作key,阅读更多关于@987654321 @。

      {information.map((item, i) => (
        <div key={i}>
          <div>{item.customer.firstName}</div>
          <div>{item.customer.lastName}</div>
        </div>
      ))}
      

      【讨论】:

      • 当我这样做时,我得到'Cannot read properties of undefined (reading 'firstName')' 这可能是因为返回的数据是数组的对象,每个数组都有一个customer 的对象firstName 和一个值。所以 .map() 返回数组并使用我们针对每个数组的索引??
      • @Jcodes 将对象数组添加到问题中,我看看我能做什么
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-12
      • 2021-11-26
      • 2021-11-24
      • 2021-12-20
      • 2021-11-27
      • 2022-01-21
      • 2021-12-04
      相关资源
      最近更新 更多