类型错误:无法读取未定义的属性(读取“类型”)

伊格纳西奥·加西亚

我正在尝试在我的预算管理应用程序中平衡收入/支出,但我的余额组件不断出现此错误:TypeError:无法读取未定义的属性(读取“类型”)。

我认为这个问题可能与我从我的减速器中带来的状态有关,该状态重复了 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');

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

类型错误:无法读取未定义的属性(读取“图像”)

类型错误:无法读取未定义的属性(读取“暗”)

类型错误:无法读取未定义的属性(读取“缓存”)

类型错误:无法读取未定义的属性(读取“reduce”)

类型错误:无法读取未定义读取“2”的属性

类型错误:无法读取未定义的属性“类型”

类型错误:无法读取未定义的属性“indexOf”,无法读取未定义的属性“toLowerCase”

类型错误:无法读取未定义错误的属性“_id”

角度:错误类型错误:无法读取未定义的属性___

错误类型错误:无法读取未定义的属性“列表”

错误类型错误:无法读取未定义的属性“标题”

错误类型错误:无法读取未定义的属性

渲染错误:“类型错误:无法读取未定义的属性‘’”

错误类型错误:无法读取未定义的属性“...”

错误类型错误:无法读取未定义的属性“填充”

错误类型错误:无法读取未定义的属性“stateName”

“类型错误:无法读取未定义的属性‘名称’”错误

错误类型错误:无法读取未定义的属性“_id”

错误类型错误:无法读取未定义的属性“getUsers”

错误类型错误:无法读取未定义的属性“doc”

**错误** 类型错误:无法读取未定义的属性“替换”

类型错误:无法在 setTimeout() 中读取未定义的属性“then”

类型错误:无法读取未定义的属性“ChangeClassName”

类型错误:无法读取未定义 NodeJS 的属性“样式”

AngularJS类型错误:无法读取未定义的属性“ GET()”

类型错误:未定义无法读取属性“webpackJsonp”

类型错误:无法读取未定义的属性“_alreadyWrapped”

opencv - 类型错误:无法读取未定义的属性“长度”

类型错误:无法读取未定义的属性“querySelector”