4

i want to render the list of users using the ngFor directive, but when i try to do this console is showing the error that.

Error

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

Group-component.ts

groupUsers: any[] = [];

this.groupService.getGroupUsers(this.groupId)
  .subscribe(groupUsers => {
    this.groupUsers = groupUsers;
  });

Group-service.ts

getGroupUsers(groupId: number): Observable<any[]> {
      return this.http.get<any[]>(this.baseUrl + 'GroupAttendance/GroupUsers/' + groupId);
}

Group-component.html

<div *ngFor="let user of groupUsers" class="modal-body">
  <div class="card">
   <div class="card-body">
    <div class="form-group">
      <p>{{ user.user.knownAs }}</p>
    </div>
  </div>
</div>

Here is the response getting the data from the server enter image description here

I don't know why console show me this error

2
  • Did you check if you're getting any data from request? Commented Feb 19, 2020 at 12:57
  • 1
    yes, data is in array format Commented Feb 19, 2020 at 17:38

3 Answers 3

1

In your group.component.html

   <div *ngIf="groupUsers?.length > 0">
    <div *ngFor="let user of groupUsers" class="modal-body">
      <div class="card">
       <div class="card-body">
        <div class="form-group">
          <p>{{ user.user.knownAs }}</p>
        </div>
      </div>
    </div>
   </div>
Sign up to request clarification or add additional context in comments.

1 Comment

1

Try this. Actually data of array is not ready to serve in html. That's why it is throwing this error. Before showing the data in html file, make sure it is ready to render. You can check it by the following way.

*ngIf="groupUsers?.length > 0" // Check that groupUsers have data and its contains data

.

<div *ngIf="groupUsers?.length > 0">

    <div *ngFor="let user of groupUsers" class="modal-body">
      <div class="card">
       <div class="card-body">
        <div class="form-group">
          <p>{{ user.user.knownAs }}</p>
        </div>
      </div>
    </div>
</div>

Comments

0

It seems that groupUsers is not an array. check again your response type.

Did you try to console.log(groupUsers) in your subscribe ?

4 Comments

i check this in console, it is an array
did you try to add an *ngIf="groupUsers" before the loop ?
Yes my dear, i tried but still the same problem persist

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.