如何修复typeerror:无法读取未定义的属性?

brqmpdu1  于 2021-09-29  发布在  Java
关注(0)|答案(2)|浏览(266)

我的ui崩溃,出现以下错误:

TypeError: Cannot read property 'filter' of undefined

at v (helper.js:16)
at i (lodash.min.js:9)
at t (lodash.min.js:59)
at value (CheckAll.js:15)

下面是helper.js代码的一个片段:

export const updatedChecked = (items, checked) => items.map(item => ({ ...item, checked }));

export const filterChecked = (items, checked) => items.filter(item => item.checked === checked);

export const moveAtTheTop = (items, itemIds) => pureReverse(itemIds).reduce((acc, v) => {
 const toCutIndex = acc.findIndex(item => item.id === v);
 const toCut = acc.splice(toCutIndex, 1)[0];
 toCut.checked = true;
 acc.unshift(toCut);
 return acc;
}, updateChecked(items, false));

export const normalizeSrcItems = items => filterChecked(items, true).map(item => item.id);
kuarbcqp

kuarbcqp1#

可能是因为你送了 undefined 在里面 items 当你打电话时,我会和你争论 normalizeSrcItems . 你需要在你叫它的地方检查一下

if (items) {
  normalizeSrcItems(items)
}

,或者检查您为什么有空项,可能是您的方式错误

11dmarpk

11dmarpk2#

显然,打电话

items.filter(...)

看起来项目未定义。快速解决方法是:

(items || []).filter(...)

但它可能无法解决问题,即项目为何未定义。

相关问题