The code:
I was trying to get all data from UsersData to print it in HTLM but I can't get it due to this error
import { Component, OnInit } from '@angular/core';
import {UserData} from "../userData";
import {UserDataService} from "../userData.service";
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
@Component({
selector: 'app-main',
templateUrl: './main.component.html',
styleUrls: ['./main.component.css']
})
export class MainComponent implements OnInit {
public userDatas: UserData[];
constructor(private dniService: UserDataService,private http: HttpClient) { }
ngOnInit(): void {
this.getUserDatas();
}
private getUserDatas() {
this.dniService.getUserDatas().subscribe(
(response: UserData[]) => {
this.userDatas = response;
console.log(this.userDatas);
},
(error: HttpErrorResponse) => {
alert(error.message);
}
);
}
}
Error:
error TS2564: Property 'userDatas' has no initializer and is not definitely assigned in the constructor.
12 public userDatas: UserData[];
What I tried:
--> public userDatas: UserData[] = []; --> public userDatas: UserData[];
Anything of that worked
CodePudding user response:
You could add the exclamation mark to the end of the variable to indicate that you willingly have it uninitialised:
public userDatas!: UserData[];
CodePudding user response:
public userDatas: UserData[] = [];
Are you sure this doesn't work? It certainly should.