During my development of an Angular 4 app using TypeScript, I encountered an error that states:
Property 'data' does not exist on type 'any[]'
This error occurs when trying to access the 'data' property in the following component method:
LoadAllUsers(): void {
this.AllUsers.Loading = true;
let AdditionalParams = {
'page': this.AllUsers.CurrentPage
};
this.UserService.All(AdditionalParams)
.subscribe(
(users) => {
this.AllUsers.Users = users.data;
this.AllUsers.Data = users;
this.AllUsers.Loading = false;
this.AllUsers.Loaded = true;
console.log ("Users response: ", this.AllUsers, this.AllUsers.Users);
},
(error)=> {
this.AllUsers.Loading = false;
console.log ("THE ERROR: ", error);
this.AllUsers.RequestStatus = error.status=0;
if (typeof error.json == 'function' && error.status!=0) {
let errorObject = error.json();
this.AllUsers.RequestError = errorObject;
} else {
this.AllUsers.RequestError = "net::ERR_CONNECTION_REFUSED";
}
this.AllUsers.HasRequestError = true;
});
}
To work around this error, I found that using
(users) => this.PrepareUsers(users)
resolves the issue.
It appears there may be something specific about TypeScript causing this. However, it is puzzling that error.status
works perfectly fine.