0

I am trying to get and display an array of JSON objects from rest API using angular frontend and rxjs.

Following is my service:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { LicenseFoodBusiness } from './license-food-business';

import { Observable, of } from 'rxjs';
import { catchError, map, tap } from 'rxjs/operators';



@Injectable({
  providedIn: 'root'
})
export class LicenseFoodBusinessService {
  private REST_API_SERVER = "http://localhost:8080/foodlicenses";
  
  constructor(private httpClient: HttpClient) { }
  
  public sendGetRequest(): Observable<LicenseFoodBusiness[]> {
    return this.httpClient.get<LicenseFoodBusiness[]>(this.REST_API_SERVER);            
    }
}

Following is my component code:

import { Component, OnInit } from '@angular/core';;
import { LicenseFoodBusinessService } from '../license-food-business.service';
import { LicenseFoodBusiness } from '../license-food-business';

import { Observable, of } from 'rxjs';
import { catchError, map, tap } from 'rxjs/operators';

@Component({
  selector: 'app-license-food-business',
  templateUrl: './license-food-business.component.html',
  styleUrls: ['./license-food-business.component.css']
})
export class LicenseFoodBusinessComponent implements OnInit {

  
  results: LicenseFoodBusiness[] = [];
  
  constructor(private lfbs: LicenseFoodBusinessService) { }
    
  ngOnInit(): void {
    this.lfbs.sendGetRequest()
        .subscribe(data => this.results = data);
      
    }
  }

Following is my model:

export interface LicenseFoodBusiness {
    LicenseId: number;
    SECP: string;
    NTN: string;
    SRB: string;
    SalesTax: string;
    NumberOfWarehouses: number;

}

Following is snippet from my componenct html:

<tr *ngFor="let licensefoodbusiness of results | async" >
        <td> {{licensefoodbusiness.LicenseId}} </td>
....

I am getting the following error:

enter image description here

Kindly help.

UPDATE:

After following the suggested edit, it does compile, but now I am not getting any data in the html table:

enter image description here

UPDATE 2 using console.log, I am getting three json objects being passed from the backend as below, but the objects arent being displayed with data is assigned to this.results.

  ngOnInit(): void {
    this.lfbs.sendGetRequest()
        //.subscribe(data => this.results = data);
        .subscribe(data => console.log(data));
      
    
  }

enter image description here

2
  • Kind of looks like you are getting empty values or the deserialization is not working. At least there are 3 rows. Try a console.log in the subscribe to see the contents. Commented Dec 13, 2021 at 9:16
  • console.log(data) shows 3 json objects Commented Dec 13, 2021 at 10:21

1 Answer 1

3

You "unwrap" the values from the observable via the .subscribe.

I think you can simply remove the | async pipe in your template.

Sign up to request clarification or add additional context in comments.

2 Comments

after removing async it did compile, but now the binded html table does not contain any data
(FYI: This would be a new question generally)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.