This is done in angular 2 and typescript.
I am trying to return data from the server and pass it back to a component in angular.
getUsers(): Promise<User[]> {
return this.http.get(this.baseUserUrl + 'GetUsers')
.toPromise()
//.then(resp => resp.json() as User[])
.then(this.returnData)
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error); // for demo purposes only
return Promise.reject(error.message || error);
}
private returnData(data: any): User[] {
var rtn = data.json() as User[];
return rtn;
}
Data is returned from the server fine and is casted to User[] fine also.
However, its not returned to the javascript method.
ngOnInit(): void {
this.userService.getUsers().then(users => this.users = users);
}
My this.users is just a bunch of promise related stuff and not the users array.