What is the correct method for storing a response in an array variable in Angular?

I am looking to save the response data from an API call in a variable and display it in the component.html file.

Component.ts file :

public coinsHistory = [];
this.service.getCoinsHistory().subscribe(
    (response) => {
        this.handleCoinsResponse(response);
    },
    (error) => {
        console.log(error);
    }
);
handleCoinsResponse(response: any) {
    console.log('what  ------', response);
    this.spinner.show();
    if (response.status === 1) {
        this.coinsHistory = response.responseData.data;
        console.log(' this.coinsHistory ------', typeof(this.coinsHistory));
    }
}

and in component.html file:

<div *ngFor="let coinData of coinsHistory">
    <p>{{ coinData.coins_earned_for }}</p>
    <p>{{ coinData.referral_coins }}</p>
</div>

Data from api is like this:


status: 1
msgCode: 224
msg: "Referrer coins history"
responseData:
    total_coins: 200
    data: Array(2)
        0:
            id: 85
            referrer_id: 4
            referee_id: null
            coins_earned_for: "Deducted for withdraw"
            referral_coins: "-100"
            created_at: "2020-02-01 12:18:21"
            updated_at: "2020-02-01 12:18:21"

        1:
            id: 85
            referrer_id: 4
            referee_id: null
            coins_earned_for: "Deducted for withdraw"
            referral_coins: "-100"
            created_at: "2020-02-01 12:18:21"
            updated_at: "2020-02-01 12:18:21"

But I encountered an error stating:

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

Answer №1

Consider specifying the answer using a model instead of using any, like this:

export interface ICoinsHistory{
totalCoins: number;
data: ICoin[];
}
export interface ICoin{
//...Details of a Coin
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Bootstrap: Arrange multiple images horizontally within a row

I am trying to design a list item for a game that includes a background, an iconholder with an icon, a title, description, and up to three resources. I have been experimenting with Bootstrap and attempted using a container-fluid with an inner row but the i ...

Tracking events in Angular 4 using angulartics2 and Adobe Analytics: A step-by-step guide

I've been working on tracking page views in my Angular 4 application, specifically with Adobe Analytics. Currently, I am utilizing angulartics2 for this purpose. First step was adding the necessary script for Adobe Staging in my index.html page: &l ...

Handling functions in Ant Design Select component with TypeScript types

I have a query. Antd offers a custom Select input with functions like onSelect, onChange, etc. I am utilizing the onSelect function which requires the following arguments: (JSX attribute) onSelect?: ((value: string | number | LabeledValue, option: OptionDa ...

"Encountering a Problem with Rendering the Datetime Picker Component in Angular

When using material-components/[email protected] with angular 14, I encountered an issue where the calendar popup sometimes renders out of place (see image below). Initially, I suspected it to be a cache problem and tried refreshing the page, which te ...

Angular 2 - Component's Off-click Feature Falters

Achieving a desired effect using Angular 2, I have implemented a component with a small popup <div>. The popup is dismissed when the user clicks anywhere on the document except for the popup itself. To achieve this functionality, I utilize HostListen ...

Guide to iterating through an Observable<Object[]> to generate an array of objects

Google Firestore collection named users is structured as follows : { "contactNumber":"0123456789", "email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="88e2e7e0e6ece7edc8efe5e9e1e4a6ebe ...

Unable to simulate axios instance in a Typescript environment

After reading through this particular article, I decided to attempt writing a unit test while simulating Axios (with Typescript). Incorporating an Axios instance to define the baseUrl. // src/infrastructure/axios-firebase.ts import axios from 'axios ...

Tips for fixing TypeScript compiler error TS2339: Issue with accessing 'errorValue' property in Angular 5 project

Within a component, I have developed a function to manage errors returned from a Rest Service and determine the corresponding error message to display to the user. This method accepts an error object (custom data structure from the service), navigates to e ...

Using Moment JS to display the days of the upcoming week

I'm in the process of developing a weather application and I need to create code that will display the upcoming week's weather forecast. The only information I have from the server is a "time" entity with a "value" set for next Monday such as "20 ...

Transmit information to Component via Modal Service

I am utilizing a Modal Service in Angular 7. To see an example, check out this StackBlitz Example. The service is used as follows: export class AppComponent { constructor(private modalService: ModalService) { } openModal() { this.modalService.o ...

Can anyone provide guidance on incorporating jQuery typing into an angular2 seed project?

I'm struggling to incorporate jQuery typings into my Angular2-seed project. Within my component, I am utilizing jQuery in the following manner: declare let $: any; export class LeafletComponent implements OnInit { ngOnInit(): void { th ...

Utilizing Angular 7 to implement table and pagination as separate entities, with distinct components

After referencing the link at https://material.angular.io/components/table/overview, I successfully implemented pagination, sort, and filter properties on a table. Now, I am looking to create a separate component specifically for pagination that will int ...

Switch up the styling of a component by updating its properties with a switch statement

Although there is a similar question, my query has a unique requirement. I have defined the common styles for my button and implemented a function using a switch statement with different properties for various buttons across different pages. However, for ...

Utilizing ternary operators in Angular 6 tables

I need to dynamically display certain amounts based on the comparison of two interest values. Here is the logic: <td *ngIf="subTable.flexitaxMaxPaymentDate"> subTable.flexitaxMaxInterest > subTable.IRDInterest ? {{subTable.maxAmou ...

What is the best way to modify URLs in angular?

As a newcomer to Angular, I am currently working on an ecommerce project where my boss has requested URL rewrite behavior. For example. The website domain is abc.com and all the products are listed on the productlisting page abc.com/productlisting So, ...

Display or Conceal Multiple Divisions Using Angular 2

I am looking to implement a functionality using Li lists to toggle the visibility of multiple divs in Angular 2. Initially, all divs on the page will be visible. When viewing on a smaller screen, I want to hide some divs and show a specific div when a cor ...

Unlocking the Power of Authorization Code Grant with PKCE in Angular6+ Applications

Seeking guidance on how to implement the MSAL library in Angular 6+ applications for integration with AZURE AD. After reviewing Microsoft's documentation, I discovered two flows: the 'implicit grant flow' and the 'auth code flow'. ...

Here is a way to trigger a function when a select option is changed, passing the selected option as a parameter to the function

Is there a way to call a function with specific parameters when the value of a select element changes in Angular? <div class="col-sm-6 col-md-4"> <label class="mobileNumberLabel " for="mobilrNumber">Select Service</label> <div cla ...

What category does a Fresh of Deno event fall under?

I'm currently working with Deno and fresh. When it comes to events in islands, what is the type for an event like the one below? export function Sample() { return ( <input type="file" onChange={(e) => ...} // What typ ...

Angular Object

I am currently working on a small Angular project that focuses on displaying and managing bank accounts using CRUD operations, including transactions. However, I have encountered an issue that is puzzling me. Whenever I click on one of the listed accounts, ...