Array.Filter and find not giving error angular 4

angular typescript

46 观看

1回复

212 作者的声誉

I have a list and I just want to filter. Array.find and filter are all producing errors.

Function :

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

} Error :

ERROR TypeError: Cannot read property 'filter' of undefined
    at AddEditRestockComponent.webpackJsonp.../../../../../src/app/restock/add-edit-restock/add-edit-restock.component.ts.AddEditRestockComponent.setProductRelatedInfo 
作者: user3701188 的来源 发布者: 2017 年 12 月 27 日

回应 (1)


0

142712 作者的声誉

This should occur if your array is not initialized, in order to handle initalize your array

suppliers: SomeClass[] = [];

and inside the method

setSupplierDetails(supplierId) {
if(this.suppliers && this.suppliers.length > 0){
const supplier = this.suppliers.filter(tempSupplier => tempSupplier.id === supplierId)[0];
}
作者: Sajeetharan 发布者: 27.12.2017 05:48
32x32