在此部分代码中显示此错误:Cannot read property 'filter' of undefined
contrat: Contrat[];
gpss: GPS[];
homeboxp: HomeboxP[];
sensors: Sensors[];
homebox: Homebox[];
getProductName(productid: string) {
const [filteredProd] = this.contrat.filter(pt => pt.contrat_id === productid);
const [filteredProdG] = this.gpss.filter(pt => pt.gps_id === productid);
const [filteredProdhbp] = this.homeboxp.filter(pt => pt.homeboxpackage_id === productid);
const [filteredProdh] = this.homebox.filter(pt => pt.homebox_id === productid);
const [filteredProds] = this.sensors.filter(pt => pt.sensors_id === productid);
if (typeof filteredProd !== 'undefined' && productid === filteredProd.contrat_id) {
return filteredProd.contratdesc;
} else if (typeof filteredProdG !== 'undefined' && productid === filteredProdG.gps_id) {
return filteredProdG.gps_serial;
} else if (typeof filteredProdhbp !== 'undefined' && productid === filteredProdhbp.homeboxpackage_id) {
return filteredProdhbp.serial_number;
} else if (typeof filteredProdh !== 'undefined' && productid === filteredProdh.homebox_id) {
return filteredProdh.serial_number;
} else if (typeof filteredProds !== 'undefined' && productid === filteredProds.sensors_id) {
return filteredProds.sensor_serial;
}
}
你能说服我吗,这部分有什么问题?工作得很好,但显示此错误
提前致谢
答案 0 :(得分:2)
在使用数组之前,必须先对其进行初始化:
contrat: Contrat[] = [];
gpss: GPS[] = [];
homeboxp: HomeboxP[] = [];
sensors: Sensors[] = [];
homebox: Homebox[] =[];