Issue
I am trying to disable automated response parsing into json
for my get
request in my HttpClient
following the API. However, every time I set
return this.http.get(requestURL, {
responseType: 'text'
}).pipe(
map((response: Response) => {
console.log(response);
})
);
my VSCode terminal flares up the pipe
and including bracket in red and I get
error TS2345: Argument of type 'OperatorFunction<Response, void>' is not assignable to parameter of type 'OperatorFunction<string, void>'
??? I am following the documentation. Am I on the wrong version?
------ EDIT ------
After suggestion below I get this
map((response: Response) => {
return this.parseData(response.text());
})
error TS2345: Argument of type 'Promise<string>' is not assignable to parameter of type 'string'.
[ng] Type 'Response' is not assignable to type 'string'. Did you forget to use 'await'?
Where parseData
takes string as input.
Solution
The issue is that you are not returning anything from the RxJS operator map. At minimum you would need to return response
:
return this.http.get(requestURL, {
responseType: 'text'
}).pipe(
map((response: Response) => {
console.log(response);
return response;
})
);
This would be similar to Array.prototype.map
and were not returning anything, you'd likely have some sort of compile error in terms of TypeScript.
Realistically, you do not need map
at all here as it used for transforming/projecting/mapping the emitted values. If you are trying to do side-effect operations such as console.log()
, you should use an operator such as tap instead:
return this.http.get(requestURL, {
responseType: 'text'
}).pipe(
tap((response) => {
console.log(response);
})
);
Update:
It sounds like you are receiving a new error regarding trying to pass a Promise<string>
to a function that expects just string
. This is coming from response.text()
. If you absolutely need to execute method text()
, you can use an operator such as switchMap to resolve the promise:
return this.http.get(requestURL, {
responseType: 'text'
}).pipe(
switchMap((response) => response.text()),
map((text) => this.parseData(text))
);
Hopefully that helps!
Answered By - Alexander Staroselsky
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.