0

I am trying to bind Data which is coming from API, Data is showing in the network but When I am trying to bind data it is giving me an error -

Cannot find a differ supporting object
function'. NgFor only supports binding to Iterables such as Arrays.

API Data in Network

 [{deliveryid: 0, frequency: "", ContactName: "Aa", Email: "[email protected]",…},…]
     
//While expanding the response

{deliveryid: 0, frequency: "", ContactName: "Aa", Email: "[email protected]"}

.TS

  public MyDigestEmailIdPrint = [];
  DigestEmailIdPrint() {
    var postData = {
    clientid: localStorage.getItem("storageselectedclient"),
  };

    this.article.DigestEmailIdPrint(postData).subscribe(
    (res) => { 
    if (res.message != "No Record Found") {
       this.MyDigestEmailIdPrint.push(res);
    }
  },
  (err) => {
    console.log(err);
  }
);

}

.HTML

<tbody>
  <tr *ngFor="let details of MyDigestEmailIdPrint">
    <td> {{details.ContactName}}</td>
    <td> {{details.Email}}</td>
    <td> {{details.frequency}}</td>
    <td><input type="checkbox"></td>
  </tr>

</tbody> 
3
  • The problem is that you use ngFor for an object which, as the error states, is not an iterable. Your backend is not giving you an array, but an object. Commented Jun 9, 2022 at 7:06
  • So How will I change or fix this ? Commented Jun 9, 2022 at 7:07
  • stackblitz.com/edit/… Commented Jun 9, 2022 at 7:13

2 Answers 2

1

DigestEmailIdPrint is a function and doesn't return anything. Maybe you should use MyDigestEmailIdPrint in the ngFor but as pointed out in the comment, the data you get from the backend is not an array.

you can fix it like this:

if (res.message != "No Record Found") {
    // use the spread syntax to create a new array and trigger change detection
    this.MyDigestEmailIdPrint = [...res.result];
}

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

8 Comments

error is gone but getting blank data, nothing is showing now on HTML page but data is in Network showing.
are you pushing the right data in the array? what is res? also please stick to code conventions variables and functions shoudl be in camelCase starting with a lovercase
Pushing the res to MyDigestEmailIdPrint will create [[{deliveryid: 0, frequency: "", ContactName: "Aa", Email: "[email protected]"}, {deliveryid: 0, frequency: "", ContactName: "Aa", Email: "[email protected]"}]] something like this. That is not correct to data get in the view.
res is a response getting from API which is now {code: 200, result: Array(5)} and if we expand this the data will be shown [{deliveryid: 0, frequency: "", ContactName: "Aa", Email: "[email protected]"}, {deliveryid: 0, frequency: "", ContactName: "Aa", Email: "[email protected]"}]
well in that case you should do : ` this.MyDigestEmailIdPrint = [...res.result];`
|
0

I would suggest you to check the res type. It seems it must be an object and thats what the error is pointing to NgFor only supports binding to Iterables such as Arrays.

The error is not only linked to this, the question is when are you calling the API and does the view load till that time?

Solution 1: If your response in the component is of different type, you can covert it to array.

Solution 2: Check when the data is getting loaded into the view. You should know the lifecycle hooks to get the data at correct time to get things in component.

Solution 3:


if (res.message != "No Record Found") {
    // node need to push data to MyDigestEmailIdPrint
    // this.MyDigestEmailIdPrint.push(res); // add the data in the array
    this.MyDigestEmailIdPrint = res; // if res is an array it is fine to assign
}

5 Comments

Yes I am getting error while assign to this.MyDigestEmailPrint = res; is Only arrays and iterables are allowed Also noted that the data is res is like this (5) [{…}, {…}, {…}, {…}, {…}]
Have you checked the type of res?
thank you All I did is this.MyDigestEmailIdPrint = [...res.result];
Yes, thats what solutions1 refers to. The res is of type object and you were getting results in result - this is because of the http response.
You don't even use spread operator to get the values, you can directly assign values: just a tip.

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.