Angular's NgFor directive is designed to work with Iterables like Arrays for data binding

After receiving data from the server, it looks like this:

{
    7281: [//post id
        {
            tags: [
                {
                    name: 'name',
                    link: 'link'
                },
                {
                    name: 'name',
                    link: 'link'
                }
            ]
        }
    ]
}

The file postlist.ts has a property declaration:

private postlists: Observable<any>;

This is the method used to fetch the JSON:

loadData() {
var
    url: string = '...';

    this.http.get<any>(url)
    .subscribe(
        data => {
            this.postlists = data;
        },
        error => {
            console.log(error);
        }
    );

}

In the template file, we render the received object as follows:

<div sino-item *ngFor="let post of postlists">
    <div *ngFor="let data of post">
        <div *ngFor="let tag of data.tags">
            <p><a href="{{tag.link}}">{{tag.name}}</a></p>
        </div>
    </div>
</div>

An error message occurs:

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

What could be causing this issue?


Despite making corrections, when building, Ionic throws the following error:

ERROR in src/app/postlist/postlist.page.html:7:78 - error TS2769: No overload matches this call.
  The last overload gave the following error.
    Argument of type 'Observable<any>' is not assignable to parameter of type 'Map<unknown, unknown>'.
      Type 'Observable<any>' is missing the following properties from type 'Map<unknown, unknown>': clear, delete, get, has, and 7 more.

7   <div *ngFor="let post of postlists | keyvalue">
                                                                               ~~~~~~~~~

  src/app/postlist/postlist.page.ts:8:16
    8   templateUrl: './postlist.page.html',
                     ~~~~~~~~~~~~~~~~~~~~~~
    Error occurs in the template of component PostlistPage.

[ERROR] An error occurred while running subprocess ng.

Answer №1

the data item on the exterior is in object form. To iterate through it using ngFor, you need to pass arrays. An option in Angular is to use the keyvalue pipe which can convert the object into an "array" of keys and values. Here's a modified version of the code incorporating the keyvalue pipe:

<div sino-item *ngFor="let post of postlists | keyvalue">
    <div *ngFor="let data of post.value">
        <div *ngFor="let tag of data.tags">
            <p><a href="{{tag.link}}">{{tag.name}}</a></p>
        </div>
    </div>
</div>

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

Access denied: Unable to rename directory '/usr/local/lib/node_modules/expo-cli' due to permission restrictions

After encountering an error that appeared to be related to permissions, I spent some time troubleshooting and finally found a solution. I wanted to share it here in case it can help others facing the same issue. If anyone has alternative solutions, please ...

Angular: Error encountered due to a SyntaxError caused by an unexpected token < appearing in JSON at position 0

Hey there, I need some assistance. I've created an Angular service but when I try to access data from my JSON file, I keep encountering errors. I've tried several solutions without success. Here is the code in app.component.ts: import {Comp ...

The issue arises when a variable inside a mat dialog text is not defined properly within an Angular application

I have utilized angular to create a dialog for gathering information and saving it on my back-end. The issue arises when attempting to send the data to the back-end using the post method, as the variable "val" remains undefined. The specific variable in q ...

What is the best way to incorporate data from a foreach method into a function call within an HTML string?

Having trouble calling a function with data from a foreach loop while generating HTML cards and buttons from an array. The issue seems to be in the renderProducts() method. /// <reference path="coin.ts" /> /// <reference path="prod ...

Sidenav Angular Material cdkScrollable is an effective tool for creating scrollable

In Angular Material CDK, there is a special Directive called CdkScrollable that allows you to monitor ScrollEvents within a specific container. I am currently attempting to retrieve the CdkScrollable associated with the default MatSidenavContent. Unfor ...

Expanding the MatBottomSheet's Width: A Guide

The CSS provided above is specifically for increasing the height of an element, but not its width: .mat-bottom-sheet-container { min-height: 100vh; min-width: 100vw; margin-top: 80px; } Does anyone have a solution for expanding the width of MatBott ...

Incorporate numerous child routes into a tab using Ionic Vue.js

I'm currently facing some challenges trying to add multiple child routes to a tab in my App using Ionic with Vue.js Everything is working perfectly fine with this setup: { path: 'tab3', component: () => import('@/views/Tab3. ...

Display the autocomplete dropdown in Angular 2+ only once the user has typed at least one letter

Looking to create a customized autocomplete feature using Angular 2+ and angular material design, where the options are only displayed after at least one letter has been typed. The default behavior of the autocomplete is to toggle on focus even when the i ...

Can existing servers support server side rendering?

I am currently working on building a Single Page Application (SPA) using the latest Angular framework. The SPA will involve a combination of static HTML pages, server side rendering, and possibly Nunjucks templating engine. My dilemma lies in the fact th ...

I'm sorry, but an error happened: Module './features/colr-v1' cannot be located

I've encountered a puzzling issue while working on my Angular 8.5.5 project in Azure DevOps. It had deployed successfully before, but now I face an unhandled exception during publishing: An error message pops up that says: "Cannot find module '. ...

Dealing with the 'UNIFIED_TEST_PLATFORM' issue while trying to compile an Ionic android app that utilizes tesseract.js and capacitor core

I recently set up an Ionic Angular project and integrated Capacitor to access native code functionalities. My project utilizes tesseract.js, as well as Capacitor core and camera plugins. However, I encountered an error while building the Android project: ...

Angular 2 is all about managing validation messages within controls

I'm currently facing challenges with getting validation messages to display properly in Angular 2. The frequent updates to Angular 2 have made it difficult to find a simple and effective solution online, so please refrain from marking this as a duplic ...

A simple way to automatically fill an input field with a mask when clicking in Angular 2

When a user clicks on this span, the following action is triggered: <span data-content="15" #Fast15 (click)="enterFastTime(Fast15)" class="quick-time">15mins</span> Users can also manually input a date in the following input field. If they ...

The issue with the Angular 5 HttpClient GET-Request not closing persists

Currently, I am utilizing an Http-GET request to fetch JSON data from my backend service. Service: public storedCategories: BehaviorSubject<Category[]> = new BehaviorSubject(null); constructor() { const subscription = this.http.get&l ...

The property 'users' is not present in the type 'Global & typeof globalThis', even after making changes in global.d.ts

In my current project, I am faced with the challenge of needing to implement global variables that can be accessed from any part of the codebase. While in JavaScript I could easily achieve this with something like global.users = {}, TypeScript presents som ...

The error message "ER_BAD_FIELD_ERROR: The column 'table.column' is not recognized in the 'on clause'" is displayed

I am encountering a perplexing issue... I'm attempting to execute a query: SELECT ordenes_servicio.idorden, estatus_orden.descripcion AS estatus, tipo_orden.descripcion AS tipo_orden, usuario.nombre, ordenes_servicio.nombres_cliente, ordenes_servicio ...

Angular 7 makes it a breeze to move elements using the drag and

Is there a way to drag and drop elements from an expansion panel while still keeping them displayed within the panel after being dropped? I am looking for Angular 7 code to achieve this functionality. Your assistance is much appreciated. I attempted to dr ...

various methods for fetching array types (information retrieved through graphql)

I currently have a functional method that is structured as follows: const handleClickOnFavouriteLocation = (item: any) => { console.log('MY COORD', item.coordinates); if(data){ console.log('DATA COORD', data.personalF ...

Exploring the Fusion of GLTF Animations within Three.js in an Angular Environment

Currently, I am diving into an Angular project and have scoured the internet for solutions without any luck in determining a way forward. The challenge at hand involves 3 GLTF files portraying different facial expressions of a model (smiling, crying, laugh ...

Attempting to create a login feature using phpMyAdmin in Ionic framework

Currently, I am in the process of developing a login feature for my mobile application using Ionic. I am facing some difficulties with sending data from Ionic to PHP and I can't seem to figure out what the issue is. This is how the HTML form looks li ...