1
Id  FirstName LastName  Action
1.  Foo       Bar       Edit
2.  Happy     Kid       Edit

The json which I get from my backend is [{"id":1,"firstName":"Foo","lastName":"Bar"},{"id":2,"firstName":"Happy","lastName":"Kid"}].

Below is my HTML file -

<table datatable class="row-border hover">
  <thead>
    <tr>
      <th>ID</th>
      <th>First name</th>
      <th>Last name</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>

    <tr *ngIf="userList" >
      <td *ngFor="let user of userList">{{user.firstName}}</td>

      <td>
        <button>Edit</button>
      </td>
    </tr>
    </tbody>
</table>    

The "userList" holds my json response.

Problem :- I get the error "Error trying to diff '[{"id":1,"firstName":"Foo","lastName":"Bar"},{"id":2,"firstName":"Happy","lastName":"Kid"}]'. Only arrays and iterables are allowed".

How do I parse my json into my required ui ?

4

3 Answers 3

1

ngFor expects array or object on which it can iterate the result, but as per your problem neither of them is present.

Try using this code -

For GET Request -

return this.http.get(API_URL)
        .map(res => res.json()
        .subscribe(
            data => this.YOUR_VARIABLE = JSON.stringify(data),
            error => alert(error),
            () => TO_DO_AFTER_REQUEST_COMPLETION
         ));

For POST Request -

return this.http.post(API_URL,POST_PARAMETERS,{
            headers:HEADERS
        })
        .map(res => res.json()
        .subscribe(
            data => this.YOUR_VARIABLE = JSON.stringify(data),
            error => alert(error),
            () => TO_DO_AFTER_REQUEST_COMPLETION
         ));

This will return Json response for iteration.

NOTE -

This is a pseudo code, make amendments as per your requirement.

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

Comments

0

You JSON is invalid. try to keep

[{id:1,firstName:"Foo",lastName:"Bar"},{id:2,firstName:"Happy",lastName:"Kid"}]

If you want to keep keys as a string you need to write pipe to convert.

1 Comment

What is invalid about the JSON?
0

The json that you get in this case is just a string and not a javascript array. ngFor expects an array to iterate over but the supplied parameter is a string. You need to convert the string to javascript object by using the json() method on the response.

Example code:

return this.http.get("some-api-url")
                .toPromise()
                .then(response => response.json().data); // check this line

If you are using observables directly and not converting toPromise, even then you need to call json() method on the response. You can even use the native JSON.parse method to convert the string response to javascript object.

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.