0

While doing official Angular tutorial, I tried to make a real backend connection instead of mock Angular "server", using Spring JPA REST. REST endpoint is configured as PagingAndSortingRepository. Here is a server response from backend. Server response

I try to get this data into front-end as follows:

@Injectable({
  providedIn: 'root'
})
export class HeroService {
  private heroesUrl = 'http://localhost:8080/api/heroes';

  constructor(
    private http: HttpClient,
    private messageService: MessageService ) { }

  getHeroes(): Observable<Hero[]> {
    // TODO: send the message _after_ fetching the heroes
    this.messageService.add('HeroService: fetched heroes');
    return this.http.get<Hero[]>(this.heroesUrl); // of(HEROES);
  }

HTML code:

<h2>My Heroes</h2>
<ul class="list-group">
  <li class="list-group-item" *ngFor="let hero of heroes">
    <a routerLink="/detail/{{hero.id}}">
      <span class="badge badge-dark">{{hero.id}}</span> {{hero.name}}
    </a>
  </li>
</ul>

This code produces following error:

ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

2
  • 1
    Please show your html code Commented Sep 3, 2019 at 8:34
  • yes please show html and where you consume getHeroes() method Commented Sep 3, 2019 at 8:39

3 Answers 3

1

The response from server contains _embedded as an object. Either you need to send it as array of "heroes" or on front end have heroes variable and assign it as

this.heroes = response._embedded.heroes;

Hope it helps.

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

Comments

1

HTML use the async pipe

<table>
  <tr *ngFor="let item of getHeroes()|async">
     <td>{{item.id}}</td>
     <td>{{item.name}}</td>
  </tr>
</table>

get only heroes property from the response object using map operator.

getHeroes(): Observable<Hero[]> {
    // TODO: send the message _after_ fetching the heroes
    this.messageService.add('HeroService: fetched heroes');
    return this.http.get<any>(this.heroesUrl)
        .pipe(
            map(data => data._embedded.heroes)
        );
}

3 Comments

Property _embedded does not exist on type Hero[]
Despite of error it works, thanx. But what is the reason for async?
@Dmitry The async pipe subscribes to an Observable or Promise and returns the latest value it has emitted. check the link angular.io/api/common/AsyncPipe
0

You are iterating an object {} in *ngFor. You should iterate over an array []

Suppose your json is saved in data Based on your json, it should be something like:

<table>
  <tr *ngFor="let item of data._embedded.heroes">
     <td>{{item.id}}</td>
     <td>{{item.name}}</td>
  </tr>
</table>

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.