On my test Angular 5 web client, on this HTTP call to my Java Jax-RS REST server I expect a return of data as array of type - PlayerRecord. (It is a simple class with a few fields).
this.http.get<PlayerRecord[]>(this.url)
.subscribe(data => this.onDataSuccess(data),
err => this.errorHttp.set(err));
I am handling the data in the below function with parameter that expects data to be again an array of type - PlayerRecord:
playerRst: PlayerRecord[];
private onDataSuccess (data: PlayerRecord[]) {
this.playerRst = data;
console.log(this.playerRst.length);
console.log(this.playerRst[0]);
...
}
Now, if the result has 2 or more elements in the array, I do get the expected array of data fine, HOWEVER, if I get only 1 record from the server, 'data' is no more an array and the steps in the onDataSuccess function fail.
Can you please explain why TypeScript can't keep an 1 element array as array or what I am missing, and how to work around this? Thank you.