1
votes

I want to show the data from my backend (CodeIgniter) to my frontend (Angular 4), but this error come up:

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

This is my component:

import { BookService } from './../book.service';
import { Component, OnInit } from '@angular/core';
import { Book } from "../book";


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

  constructor(public bookService: BookService ) { }

  ngOnInit() {
    this.getBooks();
 }

  books:Book;
  getBooks(){
    this.bookService.getBooks()
    .subscribe(books=>{
      this.books = books;
    })

  }
}

This is my service:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map' ;

@Injectable()
export class BookService {

  constructor(private http: Http) {

  }

    books=[];
    getBooks(){
      return this.http.get("http://localhost/restserver/book/list_book.php")
      .map(res => res.json());
    } 


}

My .json:

{"status":true,"data":
[{"id":"1","title":"SAINS","author":"ERLANGGA","isbn":"089928778"},
{"id":"2","title":"Geography","author":"ERLANGGA","isbn":"089182372"},
{"id":"3","title":"Math","author":"Srilangka","isbn":"091283181"},
{"id":"4","title":"test","author":"test","isbn":"1283798127"},
{"id":"5","title":"AAAA","author":"BBB","isbn":"91092301290"},
{"id":"6","title":"BBB","author":"CCC","isbn":"01920192"}]}

And this is my view:

<div class="container-fluid">
  <div class="row">
      <div class="card col-md-7">
          <div class="card-body">
            <table class="table table-responsive table-striped">
              <caption>List of Books</caption>  
              <thead>
              <tr>
                <th scope="col">#</th>
                <th scope="col">Title</th>
                <th scope="col">Author</th>
                <th scope="col">ISBN</th>
                <th scope="col">Actions</th>
              </tr>
            </thead>
            <tbody>
              <tr *ngFor="let book of books" >
                <th></th>
                <td>{{book.title}}</td>
                <td>{{book.author}}</td>
                <td>{{book.isbn}}</td>
                <td>
                  <button class="btn btn-primary "> Detail </button>
                  <button class="btn btn-success " [routerLink]="['/book-edit']"> Edit </button>
                  <button class="btn btn-danger "> Delete </button>                
              </td>
            </tr>           
          </tbody>
        </table>       
    </div>  
  </div>  
  <app-book-add class="col-md-5"></app-book-add>    

2
how does your books array look like?Sajeetharan

2 Answers

5
votes

Issue :

You are assigning the whole response to books , but all you need is data.


1) First way to solve it

Change this line from in component:

this.books = books;

to

this.books = books.data;

2) Or you can also do this in template:

Change this :

<tr *ngFor="let book of books" >

to

<tr *ngFor="let book of books?.data" >
3
votes

books should be collection of Book & then set data property of returned response.

books:Book[]; //this should be change inside Component.

//service method should return data prop from it.
getBooks(){
  return this.http.get("http://localhost/restserver/book/list_book.php")
  .map(res => res.json().data);
}