I implemented an method which returns an Observable. Inside this method I am using http.post to send a request to the backend. After receiving the response, which is a JSON object, I want to store this in an Observable variable and return that variable. But somehow I didn't manage to solve that problem. In .subscribe the res variable is not stored in the postResponse variable, but I can see in the "local" console.log that the res variable has the correct value. The global console.log is empty. Furthermore I get the error:
"TS2322: Type 'ArqResponse' is not assignable to type 'Observable'" error for the return.
My code looks like this:
postARQRequest(request): Observable<ArqResponse>{
let postResponse = new ArqResponse;
const result = this.http.post<ArqResponse>(this.arqUrl, request)
.subscribe((res: ArqResponse) => { postResponse = res; console.log('shadow: ' + res)});
console.log('global: ' + JSON.stringify(postResponse));
return postResponse;
}
My questions are:
- How can I store the response body in a variable, which then can be returned?
- How can I "cast" an ArqResponse variable to an Observable variable?
.subscribe
seems to be wrong since I get:
this.arqService.postARQRequest(...).subscribe is not a function error