0

Hey guys recently I started learning some Angular.

Currently im getting the following Error:

32  this.warning.error.push(entry.name+': '+entry.error);
                 ~~~~~
src/app/dashboard/dashboard.component.ts:33:15 - error TS2339: Property 'id' does not exist on type 'Warning[]'.

33  this.warning.id.push(entry.id);
                 ~~
src/app/dashboard/dashboard.component.ts:37:13 - error TS2339: Property 'error' does not exist on type 'Error[]'.

37  this.error.error.push(entry.name+': '+entry.error);
               ~~~~~
src/app/dashboard/dashboard.component.ts:38:13 - error TS2339: Property 'id' does not exist on type 'Error[]'.

38  this.error.id.push(entry.id);
               ~~

Problem is that I defined Interfaces for both that are importet.

export interface Error {
        id: number;
        error: string;
        }

export interface Warning {
        id: number;
        error: string;
        }

As you can see in my component.

import { Error, Warning } from '../dashboard';
...
export class DashboardComponent implements OnInit {
 error: Error[];
 warning: Warning[];
...

evaluate(): void{
        for (let entry of this.status){
        if (entry.status === 0){
                this.ok = this.ok + 1;
                }
        if (entry.status === 1 && entry.value < 8){
        this.warnings = this.warnings + 1;
        this.warning.error.push(entry.name+': '+entry.error);
        this.warning.id.push(entry.wt_id);
                }
        if (entry.status === 1 && entry.value >= 8){
        this.critical = this.critical + 1;
        this.error.error.push(entry.wt_name+': '+entry.error);
        this.error.id.push(entry.wt_id);
                }
        }
}

I already tried some things i found in older posts but nothing seems to work.

Maybe some of you guys know a fix and can point it out. Maybe it's just something that I missed.

Cheers!

2 Answers2

1

You aren't pushing to the member variables error or warning but rather the properties within the variables.

  1. You either need to change the type definitions to arrays

Interfaces

export interface Error {
  id: number[];
  error: string[];
}

export interface Warning {
  id: number[];
  error: string[];
}

Component

export class DashboardComponent implements OnInit {
  error: Error;
  warning: Warning;
 
  ...
}
  1. Or push to the member variables instead. As @GunnerB. pointed out in the comments, the arrays error and warning also need to be initialized before trying to push values to it using push function.

Interfaces

export interface Error {
  id: number;
  error: string;
}

export interface Warning {
  id: number;
  error: string;
}

Component

export class DashboardComponent implements OnInit {
  error: Error[] = [];              // <-- initialize the arrays
  warning: Warning[] = [];
  ...

  evaluate(): void {
    for (let entry of this.status) {
      if (entry.status === 0) {
        this.ok = this.ok + 1;
      }
      if (entry.status === 1 && entry.value < 8) {
        this.warnings = this.warnings + 1;
        this.warning.push({id: entry.wt_id, error: entry.name + ': ' + entry.error });
      }
      if (entry.status === 1 && entry.value >= 8) {
        this.critical = this.critical + 1;
        this.error.push({id: entry.wt_id, error: entry.name + ': ' + entry.error });
      }
    }
  }
}
ruth
  • 29,535
  • 4
  • 30
  • 57
  • Beat me to it. One addition though: the two arrays need to be initialized somewhere (which at least is not shown in OPs code). Adding a `= []` to both is sufficient. – Gunnar B. Aug 04 '20 at 09:29
  • @GunnarB.: Yes an often overlooked point. I've edited the answer. Thanks. – ruth Aug 04 '20 at 09:30
  • nice one @Michael D really good and seems to work ;) –  Aug 04 '20 at 09:30
0

Please change to: `this.warning.error = entry.wt_name + ': ' + entry.error;

error in Warning interface is string not array.

Raz Ronen
  • 2,418
  • 2
  • 14
  • 26
  • But i need it to be an Array. So i guess i should change it to `error: Error[] = [];` ? @Raz Ronen –  Aug 04 '20 at 09:23
  • correct. And have: `export interface Warning { id: number[]; error: string[]; }` – Raz Ronen Aug 04 '20 at 09:28
  • Ah very good. Nice to know multiple ways. Thanks for your involvement ! –  Aug 04 '20 at 09:33