I know there is number of questions regarding this problem, but non of them helps to solve my problem. Subscribing to the service from component, it is always return undefined.. loosing my mind over this.
component:
ngOnInit() {
this.dataService.getAllPartners().subscribe(data =>
{this.partners = data
console.log("data " + data);},// i can see the data returns
error => {
LoggerService.error('Failed to load partners.')
});
console.log("Partners " + this.partners);// partners is undefined
}
service:
private serviceUrl = '/partners';
private headers = new HttpHeaders()
.set('Content-Type', 'application/json');
constructor(private httpClient: HttpClient) { }
/**
* Fetch partners from the server
*/
getAllPartners(searchParams?: Object): Observable<Partner[]> {
return this.httpClient.get(Constants.BASE_SERVICE_URL + serviceUrl , {
params: HttpRequestService.buildRequestOptions(searchParams),
headers: this.headers
})
}
See Question&Answers more detail:os