Issue encountered while iterating over an array using NgFor

I am currently facing an issue while attempting to create a table for viewing certificates in an account using Angular. The error I am encountering is as follows:

ERROR Error: Error trying to diff '[object Object]'. Only arrays and iterables are allowed

Below is the object returned from the API:

{
  "value": [
    {
      "properties": {
        "key": {
          "keyVault": {
            "name": "AzureSdkTestKeyVault",
            "id": "/subscriptions/34adfa4f-cedf-4dc0-ba29-b6d1a69ab345/resourcegroups/testResourceGroup/providers/microsoft.keyvault/vaults/<keyVaultName>",
            "type": "Microsoft.KeyVault/vaults"
          },
          "keyName": "<keyName>",
          "keyVersion": "87d9764197604449b9b8eb7bd8710868"
        },
        "publicCertificate": "<publicCertificate>",
        "createdTime": "2017-03-06T20:33:09.7022471Z",
        "changedTime": "2017-03-06T20:33:09.7032076Z"
      },
      "id": "/subscriptions/34adfa4f-cedf-4dc0-ba29-b6d1a69ab345/resourceGroups/testResourceGroup/providers/Microsoft.Logic/integrationAccounts/testIntegrationAccount/certificates/<integrationAccountCertificateName>",
      "name": "<IntegrationAccountCertificateName>",
      "type": "Microsoft.Logic/integrationAccounts/certificates"
    }
  ]
}

Here is the model structure:

export interface Certificate {
  value?: (ValueEntity)[] | null;
}
export interface ValueEntity {
  properties: Properties;
  id: string;
  name: string;
  type: string;
}
export interface Properties {
  publicCertificate: string;
  createdTime: string;
  changedTime: string;
}

This is the component code:

export class CertificateTableComponent {
  constructor(private _integrationAccountService: integrationAccountService) {
  }

  listcertificates:Certificate[] = [];

  ngOnInit() {
    this._integrationAccountService.getcertificates()
    .subscribe
    (
      data =>
      {
        this.listcertificates = data;
      }
    );
  }
}

The section where I am looping through the data in the table:

<table>
 <tr>
   <th>Public Certificate</th>
   <th>Id</th>
   <th>Name</th>
   <th>Type</th>
 </tr>

 <tr *ngFor="let cert of listcertificates">
   <td>{{cert.properties.publicCertificate}}</td>
   <td>{{cert.id}}</td>
   <td>{{cert.name}}</td>
   <td>{{cert.type}}</td>
 </tr>
</table>

I have tried different approaches with the model and attempted to access various values from the response, but I seem to be missing something crucial. Any assistance would be highly appreciated.

Thank you!

EDIT Below is the associated service:

@Injectable()
export class integrationAccountService
{
  constructor(private httpclient: HttpClient, private api: ApiService) { }

  getcertificates(): Observable<any> {
    const httpOptions : Object = {
      headers: new HttpHeaders({
        'Authorization': 'Bearer Token Here'
      }),
      responseType: 'json'
    };
    return this.httpclient.get('URL here', httpOptions);
  }
}

Answer №1

To properly display the data from the listcertificates object, you need to iterate over its value property. It's important to wrap this iteration in an *ngIf statement because during component initialization, the listcertificates object may not yet have a value property available. Here is an example of how it can be done:

<ng-container *ngIf="listcertificates && listcertificates.value && listcertificates.value.length"
  <tr *ngFor="let cert of listcertificates.value">
    <td>{{cert.properties.publicCertificate}}</td>
    <td>{{cert.id}}</td>
    <td>{{cert.name}}</td>
    <td>{{cert.type}}</td>
  </tr>
</ng-container

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

Is it feasible to integrate gMaps with Angular2? How can I go about accomplishing this task?

In developing a typeahead service for cities and similar functions, I require the use of a typeahead directive (ng2-bootstrap) with an array of strings provided by the service. Google Maps is my chosen solution for this task. Below is code extracted from ...

Angular - Dividing Functionality into Multiple Modules

I am currently working with two separate modules that have completely different designs. To integrate these modules, I decided to create a new module called "accounts". However, when I include the line import { AppComponent as Account_AppComponent} from &a ...

Yep, identifying InferType optional attributes

Here's an example of a Yup schema I created to fetch entities known as Parcels: export const FindParcelsParamsSchema = Yup.object({ cursor: Yup.number().optional(), pageSize: Yup.number().positive().integer().optional(), }); All fields are option ...

What could be causing ngClass to constantly invoke a function without end?

I am currently working on a feature to add a class based on the presence of a value in storage or a specific set value. I am using [ngClass] to achieve this, but for some reason, the function that checks the storage is being called indefinitely. What could ...

"Learn the steps for accessing the most recent version of a ReactJS application from the server

I have my react app hosted on a Netlify domain. How can I ensure that users who have previously loaded older versions of the app are redirected to the most recent deployed version? ...

mat-sidenav is exempt from the fxFlex rules

Having trouble centering the content within . I've attempted various rules but nothing seems to be working. Interestingly, when I apply fxFlex rules to , everything falls into place perfectly. I've gone through trying to set rules for each elemen ...

Invoke a function within a component, within that very component

Hey there, I've got an Angular 2 component with a @HostListener. My goal is to call a method from this component using the @HostListener. Check out the code snippet below for my attempt at implementing this: The Component import { Component, Host ...

Using conditional buttons with ngSwitch in conjunction with ngFor to render a list of buttons from an asynchronously pip

I can't seem to figure out a logic error I am experiencing. Apart from the issue with my code, why does Angular restrict me from doing this? The purpose of using the conditional switch is to have drop-down menus for different models such as Gender. I ...

Experience the magic of changing backgrounds with Ionic 4's dynamic image feature

I am currently facing an issue while attempting to add multiple background images in my Ionic 4 project. I have successfully created styles for static images, but when it comes to dynamic images, I encounter errors with the styles. <ion-content st ...

Tips on creating a unit test for validating errors with checkboxes in your code base

In a certain scenario, I need to display an error message when a user clicks on the Next button without agreeing to the terms. To achieve this, I am looking to write a unit test case using Jest and React Testing Library. How can I go about doing this? im ...

Challenges arise when configuring routing in angular, nginx, and docker and encountering issues upon

Currently, I have set up angular with nginx as a reverse proxy within a docker container. The issue I am facing is related to the routing of the angular application. While I can smoothly navigate through various pages using the navbar, reloading the page ...

Ways to delete an element from an array in MongoDB

I am a beginner in the MEAN stack development. I am currently working on implementing this and this. I have been using the $pull method, but it seems that it is not working for me. I suspect that the issue might be due to the differences in my MongoDB stru ...

The blur event in Angular Material's mat-date-range-input does not trigger if the End Date is not selected

I am currently utilizing Angular 15 along with Angular Material 14. The code for the DatePicker control is shown below. <mat-form-field class="datepicker-widget" appearance="fill"> <mat-date-range-input [formGroup]="d ...

Angular `build` is encountering an error when dealing with externals that are declared using `webpack`

When working with Angular, I successfully load jquery from an external source. However, after the build process, I encounter a troubling error message: Uncaught ReferenceError: jQuery is not defined. It's worth noting that my build does download the j ...

Improving Image Loading Efficiency in Next.js - Enhancing Performance by Preloading Images

Currently, I am working on a project in Next.js that involves a scroll-based image loading component. The method I am using to fetch and load images dynamically based on the scroll position is causing performance problems and leading to a less than ideal u ...

Understanding type inference in TypeScript

I'm attempting to grasp the concept of inferring generics in Typescript, but I can't seem to figure out where I'm going wrong. Although my concrete example is too large to include here, I've provided a link to a small TypeScript playgro ...

Issue with Angular: Mobile view toggle button in the navbar is unresponsive

While the dropdowns are functioning correctly in web mode, the navbar toggle isn't working as expected in small screens or mobile mode. I've been trying to figure out the issue by referring to code from CodePen that I am learning from. Despite i ...

Leverage the power of the MEAN stack with Angular 2 to seamlessly retrieve data from multiple APIs

Using angular2, nodejs, expressjs, and mongodb for development. I need all APIs to fetch data and display it on an HTML page. Below is the code snippet from my .ts file. view image description here All APIs have been tested and are s ...

Angular - Implementing *ngIf based on URL parameters

Is it possible to display an element based on specific queryParams included in the URL? For example: ngOnInit() { this.route.queryParams.subscribe(params => { console.log(params); }); } If I want to achieve something similar to this: ...

Angular - Strategies for Handling Observables within a Component

I am new to Angular and recently started learning how to manage state centrally using ngRx. However, I am facing a challenge as I have never used an Observable before. In my code, I have a cart that holds an array of objects. My goal is to iterate over the ...