Angular error message TS2339 is stating that there is no property called 'data' available on the specified Object type

Hi there, I'm just starting out with Angular and I've run into a bit of a problem. An error keeps popping up saying that the property 'data' can't be found in the API object I'm trying to retrieve data from. I'm fetching data from a simple API and attempting to display it on my page. Here's the code snippet:


export class ArticlesComponent implements OnInit {
 users: Object;

  constructor(private data: DataService) { }

  ngOnInit(): void {
    this.data.getUsers().subscribe(data=>{
      this.users=data;
      console.log(this.users);

    });
  }
}

This is how the Template looks:


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

I've been stuck on this issue for quite some time now. When I check the console log, I can see that there is indeed a 'data' property containing all the user information that I want to display. However, it keeps telling me that 'data' doesn't exist. Here's the link to the API: . Any help would be greatly appreciated.

Here's the function in the service class:


export class DataService {

  constructor(private http: HttpClient) { }
  getUsers(){
       return this.http.get('https://reqres.in/api/users?page=2');
  }
}

Answer №1

I have tested your code and it worked perfectly. If you continue to encounter errors, try defining it as users?.data in the ngFor loop.

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

VS Code couldn't locate a definition for the function 'myMethod'

For some reason, I am unable to find a definition for 'myMethod' in VS Code. In my angular projects, after importing one project into VS Code, I can easily navigate to definitions using the 'F12' key. However, when I import another pro ...

Discovering the Type Inference of Function Composition Method (Chaining) in TypeScript

I'm working on implementing a function that can add a chainable method for function composition. Here's what I have so far: Also see: TypeScript playground { const F = <T, U>(f: (a: T) => U) => { type F = { compose: <V ...

Stepping up the Angular AuthGuard game: Harnessing CanMatchFn and CanActivateFn for ultimate security (Class Guards make way for Functional Guards)

I have developed an Angular AuthGuard component as shown below: @Injectable({ providedIn: 'root', }) export class AuthGuard implements CanActivate, CanLoad { constructor(private authService: AuthService, private router: Router) {} ca ...

You cannot call this expression. The type 'String' does not have any call signatures. Error ts(2349)

Here is the User class I am working with: class User { private _email: string; public get email(): string { return this._email; } public set email(value: string) { this._email = value; } ...

Having trouble with the setRoot() function not working properly in IONIC 3 after dismissing a modal menu. Any suggestions for a solution

Here is the code snippet I am using to display a modal from the HomePage. import { SignaturePage } from '../signature/signature' import { NavController, NavParams, ModalController } from 'ionic-angular'; @Component({ selector: &apo ...

The functionality of the edit page pop-up in Angular 6 seems to be malfunctioning after clicking the button for editing

I am currently working on a CRUD application in Angular 6. My task involves clicking the edit button within a populated table, which then opens a popup window for editing the data. Once the data is edited and saved, the table should update accordingly usin ...

Assigning Python Variables for Handling JSON Data

I need assistance with assigning variables to the JSON data received from a REST API. Here is the URL of the documentation I am referencing: Here is an example of the Python code I am using: response = requests.get("https://{0}{1}".format(session.get("s ...

What is the proper way to use the setTimeout function in JavaScript?

Given the code snippet below, I am tasked with printing "B" and "C" after "A" in the console after 3 seconds. My attempt was to use: setTimeout(main(),3000); However, I did not receive any output. function sleep(){ } function main(){ console.log(&ap ...

Utilize and store images based on the individual user's preferences with PlayCanvas

Currently, I am immersed in a PlayCanvas endeavor where I am trying to render specific objects with textures of my choice. The main issue arises when I come across the config.json file in the PlayCanvas build. Within this file, there is a designated path ...

Showing JSON Array Values in a Table

I have created an array and am attempting to display its values in a table. Initially, my solution only displayed a single value from the array that matched an exact ID. You can see this implementation at (). Entering "jjones" would yield a result. I then ...

CentOS 5 supporting JSON PHP version 5.3.23

I've encountered an issue while attempting to install and configure json. The current error I am facing is as follows: # PHP warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/json.so' - /usr/lib64/php/modules/jso ...

Utilizing jQuery to access JSON data using dynamic variables and storing the retrieved results in an array

I've been working on a gallery website project that features multiple categories, each with a selection of items. My approach involves using two variables to track the current category (currentCategory) and item (currentItem), then utilizing jQuery t ...

Having trouble making JSON work alongside Ajax and jQuery?

In my JavaScript, I have the following code snippet... $.ajax({ type: 'POST', url: 'http://www.example.com/ajax', data: {email: val}, success: function(response) { alert(response); } }); The PHP fil ...

A guide on organizing an array of objects by a specific property using a separate array

Here is the array I am working with: var arr = [ { count: 27, dataRil: "08/06/21", subCateg: "FISH", }, { count: 22, dataRil: "08/06/21", subCateg: "DOG", }, { count: 28, dat ...

Protecting API Key in Angular (2 and above)

I've been working on a small front end project using Angular that involves integrating Google Maps. According to the GMap documentation, I need to include a reference with an API key in my index.html page: <script type="text/javascript" src="https ...

Utilizing Restangular in a factory to interact with REST API in AngularJS

While I have a grasp on utilizing Restangular in a controller, my perception is that Restangular functions as an ORM with enhanced capabilities. An ORM should not be aware of the application's state; that responsibility lies with the controller. I b ...

Angular Module Federation upgrade error: "Schematics map not found in collection."

I am currently in the process of upgrading several Angular applications from version 12 to version 16. These applications utilize Module Federation along with the module federation plugin developed by Angular Architects. As I progress through each app and ...

Why do optional values of objects remain optional when being checked in an if statement in Typescript?

Recently at work, I encountered this code snippet and was left wondering why Typescript couldn't comprehend the logic behind it. If 'test' in the object can either be undefined or a string, shouldn't it logically infer that within an if ...

Utilizing LitJson Library in Unity with C# for JSON Parsing

Currently, I am facing a challenge while trying to convert Java code to C# particularly in the area of JSON parsing. Below is an excerpt from the Java code: mJsonObject = new JSONObject(str); Iterator<String> keys=mJsonObject.keys(); ...

Best practices for updating content within div elements using Angular's ngFor directive

My List consists of integer array type values, being displayed using *ngFor: <div *ngFor="let item of values; let i = index"> <div> <div *ngFor="let subitem of item;" style="display:block"> <div class="progress"> ...