Angular Ionic - Data not displaying because ctx.data is undefined

Viewed 265

I am trying to display data from my SQL database on a server. The data is retrieved correctly and all however it is not displaying in my application. If I console.log it displays perfectly in the console.

Interface ts:

export interface IProduct {
  productID: string;
  productImage: string;
  productName: string;
  productDescription: string;
  productRating: number;
  productPrice: number;
}

page.ts:

export class ViewProductPage implements OnInit {

  product: IProduct;

  constructor(private service: CustomerService) { }

  ngOnInit() {
    this.service.getProduct().subscribe((data: any) => {
      this.product = data;
      console.log(this.product);
    });
  }
}

HTML:

<ion-content color="dark">
  <div class="product-image-container">
    <div class="product-image-wrapper">
      <img [src]="'https://via.placeholder.com/1000'">
    </div>
    <div class="product-name">
      <h1>{{ product.productName }}</h1>
    </div>
  </div>
  <div class="product-description">
    <h6>Price:</h6>
    <p>{{ product.productPrice }}</p>
    <h6>Description:</h6>
    <p>{{ product.productDescription }}</p>
  </div>
</ion-content>

The error that gets shown in the console is:

ERROR TypeError: can't access property "productName", ctx.product is undefined

Get method:

getProduct(): Observable<IProduct> {
  return this.http.get<IProduct>(this.baseURI + 'CustomerOrder/GetProduct/' + this.productID);
}
1 Answers

As you don't initialize product and the delay of the response received, your product will be undefined.

Until the response is received, then only your product will be assigned with a value.

Solution 1: Optional chaining

According to Optional chaining,

The ?. operator is like the . chaining operator, except that instead of causing an error if a reference is nullish (null or undefined), the expression short-circuits with a return value of undefined. When used with function calls, it returns undefined if the given function does not exist.

<ion-content color="dark">
  <div class="product-image-container">
    <div class="product-image-wrapper">
      <img [src]="'https://via.placeholder.com/1000'">
    </div>
    <div class="product-name">
      <h1>{{ product?.productName }}</h1>
    </div>
  </div>
  <div class="product-description">
    <h6>Price:</h6>
    <p>{{ product?.productPrice }}</p>
    <h6>Description:</h6>
    <p>{{ product?.productDescription }}</p>
  </div>
</ion-content>

Sample Solution 1 on StackBlitz


Solution 2: Check product with *ngIf

Add *ngIf to check product so that it will not access its inner properties when product is null or undefined.

<ion-content color="dark">
  <ng-container *ngIf="product">
    <div class="product-image-container">
      <div class="product-image-wrapper">
        <img [src]="'https://via.placeholder.com/1000'">
      </div>
      <div class="product-name">
        <h1>{{ product.productName }}</h1>
      </div>
    </div>
    <div class="product-description">
      <h6>Price:</h6>
      <p>{{ product.productPrice }}</p>
      <h6>Description:</h6>
      <p>{{ product.productDescription }}</p>
    </div>
  </ng-container>
</ion-content>

Sample Solution 2 on StackBlitz

Note: Both solutions were added to delay the Observable (by 5 seconds) to reproduce the issue.

Related