I'm totally stuck with this. I have searched the internet up and down and spent at least two hours looking at SO answers on the HttpClient, without getting anywhere.
I have this basic service that should retrieve some data from a REST endpoint
import { Injectable, Inject } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class UtilityService {
public getSavedProject() {
return this.http.get(this.baseUrl + 'api/ProjectAction/RetrieveActions');
}
}
This code is then to be used in a second service where I added the following code
@Injectable({
providedIn: 'root'
})
export class ProjectTreeActionService {
//...
private data;
constructor(private utilityService: UtilityService) {}
getSavedProject() {
this.utilityService
.getSavedProject()
.subscribe((result) => this.data = result[0].toString() );
return this.data;
}
This is the most basic behaviour described in more or less every tutorial and example. However in my case the last line return this.data
always gives me an undefined
.
I already checked that the expected result is correctly retrieved by logging it to the console in the subscribe
callback, but I can't get the value written to the class property where I need it to continue working with it.
What part am I missing?