When executing the project, the information fails to appear on the screen

I am new to angular and I have been trying to use the http module to fetch data from my backend. In my user.component.html file, I have the following code:

  <tr *ngFor="let item of transporter.liste">
    <td>{{item.name}}</td>
    <td>{{item.prenom}}</td>
    <td>{{item.name}}</td>

My service looks like this:

getList():Observable<Transporter>{
return this.http.get<Transporter>("http://localhost:9000/users/list")
.pipe(
  tap(transporter=>console.log(transporter))
);

Additionally, I have defined a class:

export class Transporter{
error: string;
success: string;
liste: [];
objet: {};
constructor(error:string, success:string,liste:[],objet:{}){
    this.error=error;
    this.success=success;
    this.liste=liste;
    this.objet=objet;
}

}

Finally, here is the users component:

export class UsersComponent implements OnInit {
transporter:any; 
    constructor(private userService: UserService) {
    }
    ngOnInit(): void {
    this.onGetUsers();
    console.log(this.transporter);
    }
    onGetUsers():void{
    this.userService.getList()
    .subscribe(
    (response)=>{this.transporter=response; },
    (error)=>console.log(error),
    ()=>console.log("Done")
    );
    }

}

I would appreciate any help in understanding what might be going wrong.

Answer №1

Make sure to verify the existence of the transporter variable before accessing nested data:

<ng-container *ngIf="transporter">
  <tr *ngFor="let item of transporter.liste">
...
</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

Angular loses focus on MatTableDataSource after updates

Have you encountered the issue where the focus disappears after updating an input element in one row and clicking on the input in the next row? This happens because the datasource gets updated. I have been trying to find a way to preserve the focus while ...

The CORS policy has prevented access to XMLHttpRequest at zone.js:2969

I encountered an error while trying to use Angular to connect to a RestAPI. The zone.js:2969 is showing an error that says Access to XMLHttpRequest at 'localhost:8000/auth/api/authen' from origin 'http://localhost:4200' has been bloc ...

Guide on transferring the Token from the initial response request to the header of the second request, with the help of Axios in an Ionic React application (Making 2 Post Requests

I am trying to create a user account and save the partner's data simultaneously. The initial axios request is used to create the user and obtain a token in return. I need to pass this token as a header in the second request. Despite implementing &apos ...

Create a class where each method is required to be a "getter" function

Is it feasible to establish a class in which all methods must be getters? Possible Implementation: class Example implements AllGetters { get alpha () { } get beta () { } } Not Acceptable: class Example implements AllGetters { get alpha () { ...

Using a generic type as a value in an abstract class <T, K extends keyof T> allows for flexible and dynamic data manipulation

Currently, I am in the process of transferring some logic to an abstract class. Let's look at the abstract generic class with specific constraints: abstract class AbstractVersion< TModel extends object, TProperty extends keyof TModel, T ...

AngularJS 2: Updating variable in parent component using Router

My current app.component looks like the following: import { Component, Input } from '@angular/core'; import {AuthTokenService} from './auth-token.service'; @Component({ selector: 'app-root', templateUrl: './app ...

How can we ensure a generic async function with a return type that is also generic in Typescript?

I'm currently working on a function that retries an async function multiple times before rejecting. I want to make sure the typescript typings of the retry function are maintained and also ensure that the passed function is of type PromiseLike. Creat ...

Utilizing geolocation within a promise in Ionic 2

Our implementation of the geolocation call is done within a promise in Ionic 2. It functions properly on iOS and older Android versions. In our app.js file, we are executing the geolocation call and resolving it in the initial view. On Android Marshmallo ...

Error message in Typescript: When a class has initialized properties, a 'super' call must be the first statement in the constructor

I am currently facing some typescript errors in my project. Would you like to see a sample of the code that is causing the issue? Here is a snippet: module CoreWeb { export class Controller implements IController { public $q; ... This piece of cod ...

The property is not found within the type, yet the property does indeed exist

I'm baffled by the error being thrown by TypeScript interface SendMessageAction { type: 1; } interface DeleteMessageAction { type: 2; idBlock:string; } type ChatActionTypes = SendMessageAction | DeleteMessageAction; const CounterReduc ...

"Comparing the use of single Angular libraries versus multiple libraries on npm

I am considering consolidating all my libraries (57 in total) into a single folder called @my-organisation/team. This is because each library has many dependencies on one another and managing versioning & dependencies separately would be difficult. After s ...

How to integrate a While loop within an RXJS subscription

I have a piece of Angular code where I am attempting to subscribe to my first API and include a while loop within this subscription. Additionally, I need to subscribe to another API inside the while loop. The reason for this is that I need to subscribe t ...

In Typescript, interfaces are required to have properties written in Pascal Case instead of camel Case

Currently, I am facing a strange issue in my ASP.NET 5 application where I am using Angular 1.4 with TypeScript and RXJS. Somehow, during the runtime, all my interface properties are getting converted from camel casing to Pascal casing. This unexpected beh ...

Managing a MySQL database in NodeJS using Typescript through a DatabaseController

I'm currently working on a restAPI using nodejs, express, and mysql. My starting point is the app.js file. Within the app.js, I set up the UserController: const router: express.Router = express.Router(); new UserController(router); The UserControll ...

When navigating through a scrollable inner element, the selected menu options do not remain anchored to the parent element

When navigating through a nested element, the dropdown menu options do not move along with the page; instead, they remain fixed in position Basic HTML Layout Scrolling Behavior of Inner Element Below is the code snippet for app.component: import { ...

The appearance and functionality of the app undergo a noticeable transformation after being bundled with Webpack

After successfully migrating my Angular 2 project from SystemJS to Webpack using the latest version of Angular2-CLI, I noticed some unexpected changes in the design of the page. Despite minimal adjustments to the project files and Angular2 code during the ...

Tips for dynamically rendering a React component from an object

Looking to include an imported component with some props in my React code. Unable to find a solution on Stack Overflow for this specific format. Can someone provide guidance on how to achieve this? Thanks in advance. import { HomeIcon } from "../lib/i ...

Restricting access to tabPanel in tabView when a tab is clicked using Angular

In my tabview, I have multiple tabpanels and I am able to programmatically control it using the [activeIndex] property. However, I am facing an issue where I want to display an alert and deny access to a specific tab if a certain variable is set to false. ...

What could be the reason for the webpack:// not showing up in Chrome after running ng build?

Greetings, I'm relatively new to Angular and have noticed a difference between using ng serve and ng build. When I use ng serve, I can see webpack:// in the Chrome debugger which allows me to navigate my TypeScript files for debugging. However, when I ...

The validation using regex is unsuccessful when the 6th character is entered

My attempt at validating URLs is encountering a problem. It consistently fails after the input reaches the 6th letter. Even when I type in "www.google.com," which is listed as a valid URL, it still fails to pass the validation. For example: w ww www ww ...