Cannot read property 'healthCarePlans' of undefined (angular5)

Viewed 153

Ive looked at other anwsers to this question but havent found a solution

I'm getting the error in the title and while its not affecting the app i still would rather not have it.

<div *ngIf="currentUserData.healthCarePlans.length > 0">
    <p-carousel numVisible="3"
                [value]="currentUserData.healthCarePlans">
                    <ng-template let-insurance pTemplate="insurance">
                        <div>
                            <img class="company-header-avatar hc-img"
                                 [src]="insurance.imagePath">
                                      <div class="ui-g-12 hc-info">
                                          <div class="ui-g-6">

                          {{insurance.healthCareCompany.person.companyName}}
                                          </div>
                                          <div class="ui-g-6">
                                              {{insurance.code}}
                                          </div>
                                      </div>
                        </div>
                    </ng-template>
    </p-carousel>
</div>

I have tried the safe operator as usually that works but it doesnt seem to in this case

2 Answers

This could be because currentUserData is null , you can handle with

<div *ngIf="currentUserData && currentUserData.healthCarePlans.length > 0">

Solved it with ngif ="currentuserdata" in a external div

Related