0

I'm trying to consume json coming from a webapi. When debugging, the data is coming through correctly as per picture below:

enter image description here

Surprisingly when I try to loop through the objects in the report.subreport array, I get told it's undefined:

getReports() {
    console.log('Get Reports');
    this.staticEventsService.getReports().subscribe((rep) => {
      this.reports = rep as Report[];
      // eslint-disable-next-line no-debugger
      debugger;
      this.reports.forEach(r => {
        r.subreport.forEach(s => { console.log('subreport = ' + s.categoryId);});});
      console.log('show me the god damn reports = ' + this.reports);
    });

enter image description here

I've declared these classes to deal with the incoming data:

export class Report {
  month: number;
  subreport: SubReport[];
  };

export class SubReport {
  categoryId: number;
  occurrences: number;
}

2 Answers 2

1

Javascript is case sensitive. I see that you used subreport and it should be subReport with a capital R.

Sign up to request clarification or add additional context in comments.

1 Comment

What a waste of 5 hours of my life.🤭
1

Error came from ‘r.subreport.forEach’.Following your debug image, It should be ‘r.subReport.forEach’ (R instead of r).

I guess you have to update your Report type, or update the service response to match with your Report type.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.