最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting a Full Response (not just the body) from Angular 4 HTTP Calls - Stack Overflow

programmeradmin1浏览0评论

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:

  1. How can I resolve the above error (and why did this error occur)?
  2. 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 the getItems(): 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:

  1. How can I resolve the above error (and why did this error occur)?
  2. 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 the getItems(): 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 use http or httpclient? – 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 using HttpClient – 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 the http.get is within my service. – Skywalker Commented Oct 26, 2017 at 13:46
Add a ment  | 

2 Answers 2

Reset to default 4

HttpClient.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 )
        };
发布评论

评论列表(0)

  1. 暂无评论