The ngFor directive is unable to read the property data of type Object

I encountered an issue with Angular not recognizing the 'data' property when fetching dummy data from an API. The error message I received was: Property 'data' does not exist on type 'Object'

After attempting to remove the 'data' property and utilize "let user of users" instead, I faced another error related to the usage of 'of': Type 'Object' is not assignable to type 'NgIterable | null | undefined'

Here's a snippet of my HTML component:

<h1>Home</h1>
 
<ul *ngIf="users">
  <li value="user" *ngFor="let user of users.data"> 
    <img [src]="user.avatar">
    <p>{{ user.first_name }} {{ user.last_name }}</p>
  </li>
</ul>

Below is the TypeScript component code:

import { Component, OnInit } from '@angular/core';
import { DataService } from '../data.service';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {

  users: Object;

  constructor(private data: DataService) { 
  }

  ngOnInit() {
    this.data.getUsers().subscribe(data => {
      this.users = data
      console.log(this.users)
    })
  }
}

And here is the corresponding DataService TypeScript component:

import { Injectable } from '@angular/core';
import { HttpClient, HttpClientModule } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class DataService {

  constructor(public http: HttpClient) { }

  getUsers() {
    return this.http.get('https://reqres.in/api/users')
  }
}

Answer №1

To ensure successful compilation of your ts component, simply implement the recommended changes:

users : any ;

Alternatively, you can define the interface based on the provided sample response.

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

Encountering an error during the registration process of @fastify/middie

I am currently in the process of integrating Fastify into a small project I am working on. One of the key requirements for this project is the utilization of Middleware (via @fastify/middie). However, when I follow the necessary steps to register the middi ...

Adding a JSON array to all JSON objects in JavaScript: A step-by-step guide

Here is a JSON Object that I am working with: { "status": "CREATED", "overrides": { "name": "test_override" }, "package_name": "test", "name": "app1", "defaults": { "job": { "example": { "executors_num": "2", "fr ...

Use Node and Express with JavaScript to store HTML form data in JSON format within a .json file

Just starting out with node and express. I am capturing user input from an HTML form and attempting to append or push it in a .json file. I tried using the jsonfile npm package but the data is not being stored in an array format in the JSON file. Here is ...

Tips for traversing through jquery ui accordion when a single panel has been disabled

I currently have a jQuery UI accordion set up with Next and Previous buttons in each panel to navigate through the sections. Additionally, I have a select HTML element in one of the panels where I can choose an option. If Option 1 is selected, the second a ...

What is the process for incorporating ngTagsInput into your AngularJS and Bootstrap project?

Is there a way to prevent receiving this error message when attempting to utilize ngTagsInput? I have tried going to the module and adding: var app = angular.module("SinglePageApplication", [ "ngRoute", 'ui.bootstrap', 'ngTagsInput']) ...

All about the ins and outs of JavaScript and manipulating the

I attempted to use DOM manipulation to change the page background color and div background color by utilizing getElementById and onclick methods. I wrote some code to make this happen, but unfortunately, it did not work as expected. Can anyone identify wh ...

Unable to update FB Login button to display "Logout" after user logs in to FB app

As a junior developer, I'm currently working on integrating Facebook login functionality into my React application following the quickstart guide provided by Facebook developers. However, I've encountered some issues with making the necessary mod ...

The specified type '<T>' cannot be assigned to type '<V>'. The '<T>' type is missing an index signature

Hello, I am currently working on implementing strictTemplate in my Angular codebase and encountering some challenges while defining the types for my models. Below are the models I have: export class User { public 'name'!: Filter<string> ...

Find the matching value of the first element in an array by comparing it with values in another array using

Imagine having two different arrays const arr1 = [5, 3, 2, 7, 4]; const arr2 = [10, 9, 8, 12, 6, 5, 7]; The goal is to find and return the first matching value without resorting to using dual for loops. We need a solution that doesn't involve itera ...

The ArcGIS JavaScript ClassBreaksRenderer does not seem to be functioning properly as the layer is only displaying the

Having an issue with my ArcGIS map where only the default symbol is rendering when I add a ClassBreaksRenderer to my GeoJSONLayer. // Color Logic const low = { type: "simple-fill", color: "#fc8d59", style: "solid&q ...

What is the approach to initiating a jquery function once HTML content is dynamically added through an AJAX call?

<div id="timeline"> <ul class="grow" id="grown"><li>One</li><li>Two</li><li>Three</li><li>Four</li><li>Five</li><li>Six</li><li>Seven</li><li>Eight< ...

Angular's reactive forms retrieve the current validation rules directly from the AbstractControl

My goal is to automatically display an asterisk on an input field when a specific control has the validation rule 'required'. In my component ts file: this.form = this.fb.group({ name: ['', [Validators.required]] }); In my compon ...

Optimal approach: Utilizing JSON, JQuery, and Codeigniter to organize content within multiple tabs

In the process of developing a calendar list system, I have implemented tabbed dates at the top, with corresponding data listings underneath. While I am comfortable using JSON and JQUERY to load the data into a div, I am uncertain about how to dynamically ...

using a button as a header for a column in Vue.js

I am looking to create a clickable button as one of the headers in my table. The table receives its columns from a parent component via an array. In this case, I want to include a button as the last column header. <BaseTable :columns="table ...

Updating the condition in the function for [ngClass] in Angular 2

I am currently working on a service that pulls in a list of menu items and then uses *ngFor to load those items. The list includes the status of each menu item as well. Service buttonlist = [ {id: 1, menu: 'me', status: 'acti ...

Refining Generic Types in TypeScript

Why does the generic type not narrow after the type guard in typescript v4.4.4? Is there a way to resolve this issue? type Data = X | Y | Z type G<T extends Data> = { type: 'x' | 'y' data: T } type X = { name: string } type ...

Is it possible that bitwise left shifts in JavaScript ES6 (<<) become cyclical after surpassing a shift of 63?

From my understanding of the << bitwise left operator in JS (ES6), the void to the right is typically filled with zeros. However, through observation in both V8 and JSC, it appears that the set bits suddenly reappear when we shift by 64 or more. (2 ...

The error message "Uncaught ReferenceError: exports is not defined and require" indicates that

I am currently developing an app using angularjs and typescript, but I've encountered a persistent error that has me stumped. Below is the snippet of my code: export var NgApp = new application.Startup(); ///<reference path="../../../../../typin ...

Displaying queries using ajax in Rails

Can someone assist me in dealing with a particular issue? I am using ajax to refresh queries from the database dynamically each time there is a change in a search form. The main objective is to load N number of records based on the parameters selected in ...

Error: The specified module is missing an export that was requested

Hello, I encountered an error in my console that reads: "Uncaught SyntaxError: The requested module './components/tileHeader/controller.js' does not provide an export named 'tileHeader'". Below is the code snippet causing the issue: co ...