2

I'm having a service as follow:

  public getDoseVignettes() : Observable<DoseVignetteApi> {
    return this.apollo.watchQuery<Query>({
      query: this.VIGNETTE
    }).valueChanges
      .pipe(
        map(result => {
            return result.data.doseVignettes;
          }
        ));
  }

which returns data as expected.

This service is called as follow:


doseVignetteApi! : DoseVignetteApi;

public ngOnInit(): void {
     this.dataService.getDoseVignettes().subscribe(doseVignetteApi => {
        // console.log("VIGNETTE : " + JSON.stringify(doseVignetteApi)); [1]
        this.doseVignetteApi = doseVignetteApi;
      })
}

The console.log [1] would display these data.

The front code is:

<div fxLayout="row" fxLayoutAlign="space-between center" fxLayoutGap="20px" class="stats-cards"
     *ngFor="let vignette of this.doseVignetteApi!.vignettes!">
  <mat-card class="example-card" fxFlex="20">
    <mat-card-title>{{vignette!.aliasDose!}}</mat-card-title>
    <mat-card-content>
      <div *ngFor="let item of vignette!.items!">
        {{item.name}} : {{item!.total}}
      </div>
    </mat-card-content>
  </mat-card>
</div>

The data will display on Chrome, but got the following issue with this code:

On Chrome debugger, got : Cannot read properties of undefined (reading 'vignettes') As a consequence, the some others widgets aren't displayed (???)

Should I return an Observable from the service and use async in *ngFor="let vignette of this.doseVignetteApi!.vignettes!" or is there a way with using subscribeas in the code above ?

EDIT: Solution 1

The recommendation of @MikeOne works: the exclamation mark in the HTML template should have been replaced by a question mark.

EDIT: Solution 2

Following the recommendation of @ZrelliMajdi, got it work as follow:

<ng-container
  *ngIf="doseVignetteApi | async as dataServiceDetails">
<div fxLayout="row" fxLayoutAlign="space-between center" fxLayoutGap="20px" class="stats-cards"
     *ngFor="let vignette of this.dataServiceDetails!.vignettes ">
  <mat-card class="example-card" fxFlex="20">
    <mat-card-title>{{vignette!.aliasDose!}}</mat-card-title>
    <mat-card-content>
      <div *ngFor="let item of vignette!.items!">
        {{item.aliasVaccine}} : {{item!.totalByAlias}}
      </div>
    </mat-card-content>
  </mat-card>
</div>
</ng-container>
public ngOnInit(): void {
   this.doseVignetteApi = this.dataService.getDoseVignettes();
}
2
  • 1
    The exclamation marks in your template should be question marks.. Commented May 17, 2022 at 16:27
  • You are absolutely right. Thank you ! It works Commented May 17, 2022 at 16:32

1 Answer 1

1

You could subscribe on your observable on html template then consume its data:

<ng-container 
*ngIf="dataService | async as dataServiceDetails">
<div fxLayout="row" fxLayoutAlign="space-between center" fxLayoutGap="20px" class="stats-cards"
     *ngFor="let vignette of dataServiceDetails">
  <mat-card class="example-card" fxFlex="20">
    <mat-card-title>{{vignette!.aliasDose!}}</mat-card-title>
    <mat-card-content>
      <div *ngFor="let item of vignette!.items!">
        {{item.name}} : {{item!.total}}
      </div>
    </mat-card-content>
  </mat-card>
</div>
</ng-container>
Sign up to request clarification or add additional context in comments.

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.