1

I am using a service to get data in JSON format for my table in an Angular 7 project. This is the definition of the service.

 getLandingDashboardSnapshotDetails() {
    return this.http.get<Snapshot[]>('<removed>');
  }

This is the snapshot model used in the service

export interface Snapshot {
   RequestsPendingApproval: string,
   TotalApprovedRequests: string,
   TotalDrafts: string,
   TotalRejectedRequests: string 
}

In my .ts file, this is what I've defined.

LandingDashboardSnapshotData: Snapshot[];

ngOnInit() {
    this.messageService.getLandingDashboardSnapshotDetails().subscribe(
      response => {
        this.LandingDashboardSnapshotData = response;
        console.log(this.LandingDashboardSnapshotData)
      },
      errorResponse => { console.log(errorResponse) }
    );

This is how I'm trying to display the data in my table.

<div class="container">
    <table class='reports'>
        <tr *ngFor = "let d of LandingDashboardSnapshotData">
            <th class="reports-data">Pending for Approval : {{ d.RequestsPendingApproval }} </th>
            <th class="reports-data">Total Sent : 11</th>
            <th class="reports-data">Total Drafts : 4</th>
            <th class="reports-data"> Total Rejected : 4</th>
        </tr>
    </table>
</div>

I'm able to see the data from the service using console.log(). However, when I try to run this, I get an error saying : Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays. I don't understand why I'm getting the error. How else can I display the data?

7
  • when you do "console.log(this.LandingDashboardSnapshotData)"... you get an object array (with multiple values, right?)? Commented Dec 18, 2018 at 8:11
  • Yes, I get all the values. It looks like this : {Requests Pending Approval: 30, Total Drafts: 34, Total Rejected Requests: 36, Total Approved Requests: 34} Commented Dec 18, 2018 at 8:12
  • since you got "{Requests Pending Approval: 30, Total Drafts: 34, Total Rejected Requests: 36, Total Approved Requests: 34}" it means that you got an object only... not an array... can't use *ngFor; ... try... {{ LandingDashboardSnapshotData.RequestsPendingApproval }} {{ LandingDashboardSnapshotData.TotalDrafts }} {{ LandingDashboardSnapshotData.TotalRejectedRequests }} {{ LandingDashboardSnapshotData.TotalApprovedRequests }} and you will see the results... Commented Dec 18, 2018 at 8:15
  • I tried it. I got an error saying : Cannot read property 'RequestsPendingApproval' of undefined Commented Dec 18, 2018 at 8:18
  • if the data you got is an object containing multiple arrays... you can try <tr *ngFor = "let d of LandingDashboardSnapshotData.RequestsPendingApproval "> Commented Dec 18, 2018 at 8:18

3 Answers 3

1

in your service file

getLandingDashboardSnapshotDetails() {
    return this.http.get<Snapshot>('<removed>');
  }

and then in your component file replace LandingDashboardSnapshotData: Snapshot[]; with LandingDashboardSnapshotData: Array<Snapshot>; and then in constructor define it as an empty array, and after getting response from service try to push it in your array because you are getting object so don't need to assign it just push it in array.

export class AppComponent  {
  LandingDashboardSnapshotData: Array<Snapshot>;

constructor(private http: HttpClient) {
  this.LandingDashboardSnapshotData = [];
}

ngOnInit() {
    this.getLandingDashboardSnapshotDetails().subscribe(
      (response) => {
        this.LandingDashboardSnapshotData = response;
        console.log(this.LandingDashboardSnapshotData)
      }
    );
}
}

here is a link of working example.

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

2 Comments

This worked for me but instead of this.LandingDashboardSnapshotData.push(response); I used this.LandingDashboardSnapshotData = response; Changing the datatype of the LandingSnapshotData variable worked. Thanks for your response!
and also suggest an edit of my answer so it will help others as well.
0

It seems that your this.http.get returns something other then array.

typescript declarations are only for development side and type's are become unavailable when they are transposed to JavaScript.

Hence, your this.http.get returns [Object Object] but you are trying to iterate with NgFor which only accepts Arrays.

7 Comments

Yes, I just checked. It's returning an object of this format {Requests Pending Approval: 30, Total Drafts: 34, Total Rejected Requests: 36, Total Approved Requests: 34. How do I extract values from this? I've already tried using {{ LandingDashboardSnapshotData.RequestsPendingApproval }} and so on but in that I'm getting an error saying : Cannot read property 'RequestsPendingApproval' of undefined
your json data includes spaces which is not usual. Requests Pending Approval: 30. have a small chat with your backend developer :)
Oh no, that's just the output of console.log(). The actual JSON looks like this : {"Requests Pending Approval":30,"Total Drafts":34,"Total Rejected Requests":36,"Total Approved Requests":34}
dont you see that {"Requests Pending Approval":30 has spaces?
I got that changed from the backend. Now it is : {"TotalRejectedRequests":36,"TotalDrafts":34,"TotalApprovedRequests":34,"RequestsPendingApproval":30} I am still getting the error saying Cannot read property 'RequestsPendingApproval' of undefined
|
0

following up from your comment "No, it contains only one value for each. That is why the *ngFor isn't working..." try this...

<div class="container">
    <p class='reports' *ngIf="LandingDashboardSnapshotData">
        {{ LandingDashboardSnapshotData.RequestsPendingApproval }} <br/>
        {{ LandingDashboardSnapshotData.TotalRejectedRequests }} <br/>
        etc...
    </p>
</div>

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.