0

<img *ngIf="!selectedOrganizationDetail[0]?.logo" src='some-link' alt="call-home-care-logo" class="logo default-logo">

I am getting data inside selectedOrganizationDetail from API call. But in my HTML i have an *ngIf, and it works fine but this error "Cannot read properties of null (reading '0')" shows up in the console everytime. What should i do ?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • Use `selectedOrganizationDetail?.[0]?.logo` – adiga Nov 07 '22 at 05:28
  • 1
    it is because selectedOrganizationDetail is initially null and its takes some time to finish the API call and fill selectedOrganizationDetail. When the view gets rendered before API call is finished and it reached the *ngIf check it finds that selectedOrganizationDetail is null so it throws that error. What you can do is add a check before img tag or change `*ngIf="selectedOrganizationDetail && selectedOrganizationDetail[0]?.logo"` – Shailesh B Nov 07 '22 at 05:31

0 Answers0