error message Can not find a different supporting object '[object Object]' of type 'object'. NgFor only

0

I'm having this error message;

Thisindicatesthattheerrorishere;

<!--INíCIOdoCONTEÚDO--><sectionclass="content-header">
  <h1>
    Todos os Restaurantes
  </h1>
</section>


<section class="content">

  <div class="row">
        <div *ngFor="let restaurant of restaurants" class="col-sm-6 col-xs-12">

          <mt-restaurant [restaurant]="restaurant"></mt-restaurant>

        </div>
  </div>
</section>

This is my class of services;

@Injectable()
export class RestaurantService {
  public url: String = 'http://localhost:3000/api/';

  constructor(private http: Http) {
  //  this.url = environment.url;
   }

   restaurants(): Observable<Restaurant[]> {
    return this.http.get('${this.url}/restaurants')
    .map(response => response.json())
  }

And this is my component class;

  restaurants: Restaurant[]

  constructor(private restaurantService: RestaurantService){}

  ngOnInit() {
    this.restaurantService.restaurants()
    .subscribe(restaurants =>  this.restaurants = restaurants)
  }

What can be wrong?

    
asked by anonymous 29.06.2018 / 15:57

3 answers

0

You are passing "restaurant" and not "restaurants" which is your array.

<mt-restaurant [restaurant]="restaurant"></mt-restaurant> 

Replace with

    <mt-restaurant [restaurant]="restaurants"></mt-restaurant>
    
29.06.2018 / 16:01
0

It worked to make this change in the code;

  restaurants(): Observable<Restaurant[]> {
    return this.http.get('${this.url}/restaurants')
    .map(response => response.json().restaurants)
  }
    
29.06.2018 / 17:36
0

The problem is that ngFor expects an array, and is receiving a Response object. The error is here:

ngOnInit() {
    this.restaurantService.restaurants()
    .subscribe(restaurants =>  this.restaurants = restaurants)
}

You receive the complete Response object, you need to indicate the body of this Response for your restaurants attribute :

ngOnInit() {
    this.restaurantService.restaurants()
    .subscribe(restaurants =>  this.restaurants = restaurants.body)
}

I hope it solves your problem.

    
01.11.2018 / 15:13