I have an object with two simple numbers and then a list of objects:
export class Lineup {
players: Player[];
fantasypoints: number;
salary: number;
constructor(values: Object = {}) {
Object.assign(this, values);
}
}
I hit a service to populate it with a post (I add in some players and get back a lineup from it:
public postLineup(players): Observable < Lineup > {
return this.http.post <Lineup>(`${API_URL}/optimize`, JSON.stringify(players));
}
This POST returns the data fine and as far as I can tell it will correctly populate the JSON into the Lineup object.
However in my app.component when I try to assign these values into component variables so I can actually use the data. I can't seem to get the assignments to work.
onClickOptimize() {
this.apiService.postLineup(this.playerSource.data)
.subscribe(lineupreturned => {
this.lineupSource = new MatTableDataSource(lineupreturned.players);
this.salary = lineupreturned.salary;
this.fantasypoints = lineupreturned.fantasypoints;
this.displaylineup = true;
console.log("component output:"), console.log(lineupreturned);
});
}
The console.log of lineupreturned actually shows an object and it is populated. However, if you were to log this.salary or any other variable it comes back as undefined. I cannot for the life of me understand why none of these variables are getting populated.
EDIT: with lineupreturned variable result from console.log:
null: Array(1) [Object]
length: 1
__proto__: Array(0) [, …]
0: Object {fantasypoints: 324.15, players: Array(8), salary: 48800}
fantasypoints: 324.15
players: Array(8) [Object, Object, Object, …]
length: 8
__proto__: Array(0) [, …]
0: Object {_max_exposure: null, _projected_ownership: null, first_name: "Ray", …}
1: Object {_max_exposure: null, _projected_ownership: null, first_name: "Clearlove", …}
2: Object {_max_exposure: null, _projected_ownership: null, first_name: "Scout", …}
3: Object {_max_exposure: null, _projected_ownership: null, first_name: "Hope", …}
4: Object {_max_exposure: null, _projected_ownership: null, first_name: "Meiko", …}
5: Object {_max_exposure: null, _projected_ownership: null, first_name: "SnowFlower", …}
6: Object {_max_exposure: null, _projected_ownership: null, first_name: "Arce", …}
7: Object {_max_exposure: null, _projected_ownership: null, first_name: "Infinity eSports", …}
salary: 48800
__proto__: Object {constructor: , __defineGetter__: , __defineSetter__: , …}
lineupreturnedthat you're getting from your API.