2017-12-27 108 views
1

我有一个列表,我只是想过滤。 Array.find和filter都会产生错误。Array.Filter和找不到错误角度4

功能:

setSupplierDetails(supplierId) { 
const supplier = this.suppliers.filter(tempSupplier => tempSupplier.id === supplierId)[0]; 
this.supplierName = supplier.supplierName; 

} 错误:

ERROR TypeError: Cannot read property 'filter' of undefined 
    at AddEditRestockComponent.webpackJsonp.../../../../../src/app/restock/add-edit-restock/add-edit-restock.component.ts.AddEditRestockComponent.setProductRelatedInfo 
+4

显然,'this.suppliers'没有被定义。确保它不仅被声明,而且被初始化(例如'suppliers:SomeClass [] = [];')。 – ConnorsFan

+0

发布'this.suppliers' –

+0

我找到了答案。我通过setSupplierDetails(supplierId)将supplierId作为一个我意识到导致问题的数字。 setSupplierDetails(supplierId){供应商= this.suppliers.filter(tempSupplier => tempSupplier.id === + supplierId)[0]; this.supplierName = supplier.supplierName; – user3701188

回答

0

如果你的数组没有初始化,这应该发生,以处理initalize您的阵列

suppliers: SomeClass[] = []; 

和内方法

setSupplierDetails(supplierId) { 
if(this.suppliers && this.suppliers.length > 0){ 
const supplier = this.suppliers.filter(tempSupplier => tempSupplier.id === supplierId)[0]; 
}