user9515832
user9515832

Reputation:

ERROR TypeError: Cannot read property 'filter' of undefined in typescript

In this part code show this error: 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;
    }
  }

Can you sugest me, what is the problem in this part? Work very good, but show this error

Thanks in advance

Upvotes: 0

Views: 7357

Answers (1)

Safiyya
Safiyya

Reputation: 1393

You have to initiatize your arrays before you use them :

  contrat: Contrat[] = [];
  gpss: GPS[] = [];
  homeboxp: HomeboxP[] = [];
  sensors: Sensors[] = [];
  homebox: Homebox[] =[];

Upvotes: 3

Related Questions