admin管理员组文章数量:1432359
I'm trying to get a full response from my HTTP calls. According to the Angular documentation I should set my HTTP call "options" as {observe: 'response'}
I get the following error if I do that:
Type 'Observable<HttpResponse<IItem[]>>' is not assignable to type 'Observable<IItem[]>'. Type 'HttpResponse<IItem[]>' is not assignable to type 'IItem[]'. Property 'includes' is missing in type 'HttpResponse<IItem[]>'.
I think my concepts regarding Observables & Typing/Casting aren't that clear.
I've got a HTTP Get request which is returning an observable and Strong typing it into an array of items (See below)
getItems(): Observable <IItem[]>{
return this._http.get<IItem[]>(url, {observe: 'response'})
.do(data => console.log('All: ' + JSON.stringify(data)))
};
I tried setting getUsers(): Observable <HttpResponse<IUser[]>>
which solved the above error but within my subscribe
I got the following error when I try to assign the returned data to a local variable this.item = data;
where item
is declared as item: Item[]
Type 'Observable<HttpResponse<IItem[]>>' is not assignable to type 'IItem[]
So my questions are:
- How can I resolve the above error (and why did this error occur)?
- This might be a stupid question but if
._http.get<IItem[]>
is already setting the expected "type" of response do I still need to set thegetItems(): Observable <IItem[]>
methods return type?
I think I'm not understanding some basic concept that's causing this confusion. Any help will be appreciated. I'm new to Angular 4 so please be gentle.
I'm trying to get a full response from my HTTP calls. According to the Angular documentation I should set my HTTP call "options" as {observe: 'response'}
I get the following error if I do that:
Type 'Observable<HttpResponse<IItem[]>>' is not assignable to type 'Observable<IItem[]>'. Type 'HttpResponse<IItem[]>' is not assignable to type 'IItem[]'. Property 'includes' is missing in type 'HttpResponse<IItem[]>'.
I think my concepts regarding Observables & Typing/Casting aren't that clear.
I've got a HTTP Get request which is returning an observable and Strong typing it into an array of items (See below)
getItems(): Observable <IItem[]>{
return this._http.get<IItem[]>(url, {observe: 'response'})
.do(data => console.log('All: ' + JSON.stringify(data)))
};
I tried setting getUsers(): Observable <HttpResponse<IUser[]>>
which solved the above error but within my subscribe
I got the following error when I try to assign the returned data to a local variable this.item = data;
where item
is declared as item: Item[]
Type 'Observable<HttpResponse<IItem[]>>' is not assignable to type 'IItem[]
So my questions are:
- How can I resolve the above error (and why did this error occur)?
- This might be a stupid question but if
._http.get<IItem[]>
is already setting the expected "type" of response do I still need to set thegetItems(): Observable <IItem[]>
methods return type?
I think I'm not understanding some basic concept that's causing this confusion. Any help will be appreciated. I'm new to Angular 4 so please be gentle.
Share Improve this question edited Oct 26, 2017 at 13:01 Skywalker asked Oct 26, 2017 at 12:59 SkywalkerSkywalker 5,20417 gold badges66 silver badges127 bronze badges 4-
what do you mean by
full response
? and do you usehttp
orhttpclient
? – Max Koretskyi Commented Oct 26, 2017 at 13:00 - 1 Assuming you use HttpClient this should be what you looking for. – Shahar Galukman Commented Oct 26, 2017 at 13:03
-
@AngularInDepth. By full response I mean getting headers, status codes and everything else. Currently it's only returning the
body
. I am usingHttpClient
– Skywalker Commented Oct 26, 2017 at 13:04 -
@ShaharGalukman Thanks for the ment. Thats exactly what I did the only difference being my
subscribe
is in my ponent while thehttp.get
is within my service. – Skywalker Commented Oct 26, 2017 at 13:46
2 Answers
Reset to default 4HttpClient.get
method is defined to use generic type T
:
get<T>(url: string, options?: {
...
}): Observable<T>;
So since since you pass <IItem[]>
to the get
method here this._http.get<IItem[]>
according to the definition the get
method returns HttpResponse<IItem[]>
object. However you declare your method getItems
to return IItem[]
and hence you get the error:
Type 'Observable<HttpResponse<IItem[]>>' is not assignable to type 'Observable<IItem[]>'.
To fix it, you need to return IItem[]
. This can be done by extracting body of type IItem[]
from HttpResponse<IItem[]>
and return it. do
operator can't return anything so you have to use map
operator for that:
this._http.get<IItem[]>('url', {observe: 'response'})
.do(data => console.log('All: ' + JSON.stringify(data)))
.map(data => data.body)
this should work :
getItems(): Observable <IItem[]>{
return this.http
.get<IItem[]>(url, {observe: 'response'})
.do(data => console.log('All: ' + JSON.stringify(data)))
.map( (r: HttpResponse<IItem[]> ) => r.body )
};
本文标签: javascriptGetting a Full Response (not just the body) from Angular 4 HTTP CallsStack Overflow
版权声明:本文标题:javascript - Getting a Full Response (not just the body) from Angular 4 HTTP Calls - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745584673a2664828.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论