类型“ Object”的参数不能分配给类型“ HttpResponse <any>”的参数

时间:2019-07-26 12:30:10

标签: angular object nativescript httpclient httpresponse

  

注意:这不是重复的问题   "Type 'Object' is not assignable to type" with new HttpClient / HttpGetModule,   有关更多详细信息,请阅读下面的EDIT部分。您也可以在这里https://github.com/bestazad/confusion

找到完整的项目

我试图将NativeScript项目更新到版本6,我更改了如下代码:

process-httpmsg.service.ts:

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient, HttpResponse } from "@angular/common/http";
import { throwError } from 'rxjs';

@Injectable() export class ProcessHTTPMsgService {

    constructor() { }

    public extractData(res: HttpResponse<any>) { 
        let body = res//.json(); 
        return body || { }; 
    }

    public handleError (error: HttpResponse<any> | any) { 
        // In a real world app, you might use a remote logging infrastructure 
        let errMsg: string; 
        if (error instanceof HttpResponse) { 
            const body = error//.json() || ''; 
            const err = body//.error || JSON.stringify(body); 
            errMsg = `${error.status} - ${error.statusText || ''} ${err}`; 
        } 
        else { 
            errMsg = error.message ? error.message : error.toString(); 
        }
    return throwError(errMsg);

    }
}

文件dish.service.ts

import { Injectable } from '@angular/core'; 
import { Dish } from '../shared/dish'; 
import { Observable } from 'rxjs'; 
import { HttpClient, HttpResponse } from "@angular/common/http";
import { baseURL } from '../shared/baseurl'; 
import { ProcessHTTPMsgService } from './process-httpmsg.service'; 
import {map, delay, catchError} from 'rxjs/operators'; 


@Injectable() export class DishService {

    constructor(public http: HttpClient, 
        private processHTTPMsgService: ProcessHTTPMsgService) { }

    getDishes(): Observable<Dish[]> { 
        return this.http.get(baseURL + 'dishes')
        .pipe(map(res => { return this.processHTTPMsgService.extractData (res); }) 
        ,catchError(error => { return this.processHTTPMsgService .handleError(error); })); 
    }

    getDish(id: number): Observable<Dish> { 
        return this.http.get(baseURL + 'dishes/'+ id) 
        .pipe(map(res => { return this.processHTTPMsgService.extractData (res); }) 
        ,catchError(error => { return this.processHTTPMsgService .handleError(error); }));
    }

    getFeaturedDish(): Observable<Dish> { 
        return this.http.get(baseURL + 'dishes?featured=true') 
        .pipe(map(res => { return this.processHTTPMsgService.extractData (res)[0]; }) 
        ,catchError(error => { return this.processHTTPMsgService .handleError(error); })); 
    }
}

但是我在包含(res)对象的行中收到以下错误消息,例如以下代码行:.pipe(map(res => { return this.processHTTPMsgService.extractData (res); })

  

“对象”类型的参数不能分配给类型的参数   'HttpResponse'。 “对象”类型可分配给很少的对象   其他类型。您是要改用“ any”类型吗?

我该如何解决?

当我使用tns run andoird --device Nexus_5x运行项目时,出现以下错误:

ERROR in src/app/services/dish.service.ts(17,75): error TS2345: Argument of type 'Object' is not assignable to parameter of type 'HttpResponse<any>'.
  The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
    Type 'Object' is missing the following properties from type 'HttpResponse<any>': body, type, clone, headers, and 4 more.
src/app/services/dish.service.ts(23,75): error TS2345: Argument of type 'Object' is not assignable to parameter of type 'HttpResponse<any>'.
  The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
src/app/services/dish.service.ts(29,75): error TS2345: Argument of type 'Object' is not assignable to parameter of type 'HttpResponse<any>'.
  The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
src/app/services/leader.service.ts(19,75): error TS2345: Argument of type 'Object' is not assignable to parameter of type 'HttpResponse<any>'.
  The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
src/app/services/leader.service.ts(25,75): error TS2345: Argument of type 'Object' is not assignable to parameter of type 'HttpResponse<any>'.
  The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
src/app/services/leader.service.ts(31,75): error TS2345: Argument of type 'Object' is not assignable to parameter of type 'HttpResponse<any>'.
  The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
src/app/services/promotion.service.ts(18,75): error TS2345: Argument of type 'Object' is not assignable to parameter of type 'HttpResponse<any>'.
  The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
src/app/services/promotion.service.ts(25,75): error TS2345: Argument of type 'Object' is not assignable to parameter of type 'HttpResponse<any>'.
  The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
src/app/services/promotion.service.ts(31,75): error TS2345: Argument of type 'Object' is not assignable to parameter of type 'HttpResponse<any>'.
  The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
  

编辑

:这不是一个重复的问题,因为更改如下代码行:

return this.http.get<Dish[]>(baseURL + 'dishes')

无法解决问题,并给我一个新错误:

  

类型'Dish []'的参数不能分配给类型的参数   'HttpResponse'。类型“ Dish []”缺少以下内容   类型“ HttpResponse”的属性:正文,类型,克隆,标头,   还有另外4个。ts(2345)

0 个答案:

没有答案