1

I have this Model-class.

export class Drivefunction{
public Injection: Injection;
public df_uid_machine_injsiz:string;
public Motor_Cable_IEC: Motor_cable;
...

And in my display.component.ts I have this function which fills my Drivefunction[]

fillDrivefunctions(){
  this.machineService.getDrivefunctions(this.injectionID)
    .subscribe((df:Drivefunction[])=> {
      this.drivefunctions = df;
      console.log(this.drivefunctions)
    });
}

As you see I try a console.log to see if the json Objects a properly filled

enter image description here

all Objects and nested Objects are filled!

and in my HTML I have a ngFor with my drivefunction[] I can access properties like strings but get "cannot read property of undefined" if i try to access an nested object even if it's correctly filled.

Do I have to "extra" subscribe this nested objects?

my HTML:

<ion-list *ngFor="let drivefunction of this.drivefunctions; ">
    <ion-item> 
       <ion-label>{{drivefunction.Injection.inj_uid_machine_injsiz}}</ion-label>
    </ion-item>
</ion-list>
4
  • 1
    add the HTML to the question Commented Nov 30, 2020 at 15:37
  • @RameshReddy done Commented Nov 30, 2020 at 15:45
  • {{drivefunction.Injection?.inj_uid_machine_injsiz}} use the ? operator. If Injection isn't populated it won't try to access inj_uid_machine_injsiz. you also don't need the this in the html. Commented Nov 30, 2020 at 15:46
  • @rhavelka i know but my nested objects are undefined, but as you see the console output they aren't Commented Nov 30, 2020 at 15:48

2 Answers 2

2

I think this is a typo problem.

Based on the HTML you gave.

<ion-label>{{drivefunction.Injection.inj_uid_machine_injsiz}}</ion-label>

Bu in the console screenshot, we can see that Injection is lowercase. Try

<ion-label>{{drivefunction.injection.inj_uid_machine_injsiz}}</ion-label>

And change the Drivefunction class accordingly:

export class Drivefunction {
   public injection: Injection;
...
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, that's right. I didn't see it. +1 for you
@futuremanuel I have added details to my answer.
0

Make sure to declare your drivefunction properties as an array. It will looks like this code below:

public drivefunction: Array<Drivefunction> = new Array();

So now, you will get te result. I hope so.

4 Comments

leaving the variable undeclared wouldn't cause the "cannot read property of undefined" error
I thinks it will, if you don't use *ngIf condition and you use the loop. Baceuse you can loop undefined value of a property, when the data still waiting for response.
It doesn't, if the array is [], null, or undefined then there is nothing to loop over, so the *ngFor doesn't try to access any of the child objects. you can play around in this stackblitz
Thanks for your explaination.

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.