TypeError: Cannot read properties of undefined (reading 'type')

TypeError: Cannot read properties of undefined (reading 'type')

我正在尝试在我的预算管理应用程序中进行 incomes/expenses 的平衡,但我在我的平衡组件中不断收到此错误:类型错误:无法读取未定义的属性(读取 'type') .

我认为问题可能与我从 reducer 带来的重复状态有关,2 次是空数组 [],2 次是满的。我尝试了很多但无法修复它..知道它可能是什么吗??

这是我的余额组件代码:

export default function Balance(){

    const operations = useSelector((state) => state.operations);

    console.log(operations);
    //Shows 4 arrays, 2 empty and 2 full

    const [total, setTotal] = React.useState([]);

    const operationsListCopy = [...operations];

    React.useEffect(() => {

        let entryArray = operationsListCopy.filter((oneOperation) => oneOperation.type == 'INCOME');
        let entryArrayTotal = entryArray?.reduce((amount, item) => item.amount + amount, 0);

        let exitArray = operationsListCopy.filter((oneOperation) => oneOperation.type == 'EXPENSE');
        let exitArrayTotal = exitArray?.reduce((amount, item) => item.amount + amount, 0);
            
        let arrayTotal = entryArrayTotal - exitArrayTotal;

        setTotal(arrayTotal);
    }, [operations]);

您应该在访问前检查空值或未定义值。 你可以在这里添加条件

let entryArray = operationsListCopy.filter((oneOperation) => oneOperation?.type == 'INCOME');

这里

 let exitArray = operationsListCopy.filter((oneOperation) => oneOperation?.type == 'EXPENSE');