1

I have data service that fetch data from my api:

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { HttpClient, HttpParams } from '@angular/common/http';

@Injectable()
export class DataService {

constructor(private http: HttpClient) { }

showProducts(){
return this.http.get('http://localhost:8000/api/v1/products');
}

}

and my component

import { Component, OnInit } from '@angular/core';
import { DataService } from '../data.service';
//import { Products } from '../products';

@Component({
selector: 'app-products',
templateUrl: './products.component.html',
styleUrls: ['./products.component.css']
})
export class ProductsComponent implements OnInit {
products;

constructor(private data:DataService) { }

ngOnInit() {
this.showProducts();
}
showProducts() {
this.data.showProducts()
  .subscribe(res => this.products = res);
}

}

and my html compopnent:

<div>
<table border="1">
<tr>
  <th>Product</th>
  <th>Code</th>
  <th>Price</th>
  <th>Action</th>
</tr>
<tr *ngFor="let product of products">
    {{product.product_name}}
</tr>
</table>


</div>

the problem here that i got this error when i run my code in the browser:

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

2- ERROR CONTEXT DebugContext_ {view: {…}, nodeIndex: 21, nodeDef: {…}, elDef: {…}, elView: {…}}

4
  • Mae sure the API returns the Obsdervable of array not object Commented May 9, 2018 at 11:40
  • try to parse into JsON like this.products = res.json() Commented May 9, 2018 at 11:42
  • can you explain in more details Commented May 9, 2018 at 11:42
  • What does your res look like? Can you do a console.log(res) and show us the output? Commented May 9, 2018 at 11:55

4 Answers 4

2

Try this it may helps and see the console.log value too

 showProducts() {
          this.data.showProducts().subscribe(res => {
                 this.products = res;
                console.log(this.products);
           });
  }
Sign up to request clarification or add additional context in comments.

Comments

0

I suspect the response comming from the backend.

showProducts() {
this.data.showProducts()
  .subscribe(res => this.products = res);
}

Just check the "res" here. It might not be an array hence the products.

Comments

0

Your respond from API must be JSON format, if not you should explicitly declare respondType in your service. something like:

   apiUrl = 'http://localhost:8000/api/v1/products'
   showProducts() {
        return this.http.get<Product[]>(this.apiUrl, {observe: 'response', 
   resposeType: 'text'});
   }

it seems nothing wrong with your DOM or Component.

Comments

0

Try this way it will help and also optimized and check console log value

showProducts(): void {
  this.data.showProducts().subscribe(( this.products: any) => {
    console.log(this.products); 
    });
  }

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.