Angular ret.json不是MapSubscriber.project中的函数

Angular ret.json不是MapSubscriber.project中的函数,angular,Angular,我刚刚将我的angular 4项目升级到angular 6,并出现以下错误。有人能告诉我问题出在哪里吗 core.js:1673 ERROR TypeError: ret.json is not a function at MapSubscriber.project (SettingsController.ts:24) at MapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapSubs

我刚刚将我的angular 4项目升级到angular 6,并出现以下错误。有人能告诉我问题出在哪里吗

core.js:1673 ERROR TypeError: ret.json is not a function
    at MapSubscriber.project (SettingsController.ts:24)
    at MapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapSubscriber._next (map.js:35)
    at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:55)
    at MapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapSubscriber._next (map.js:41)
    at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:55)
    at FilterSubscriber.push../node_modules/rxjs/_esm5/internal/operators/filter.js.FilterSubscriber._next (filter.j
我注意到我有以下映射到ret.json的控制器方法。我需要在这里做任何更改吗

export class AdminProxy {
        constructor(private _phttp: ProxyHttp) { } 


    upload(input: BinaryTransferObject<Dto.UploadInfo>): Observable<ApiResult<Dto.IdResponse>> {
        return this._phttp.post(`/platform/admin/upload`,input||{}).pipe(map((ret:Response) => <ApiResult<Dto.IdResponse>>({  data: ret.json() , originalResponse: ret })));
    }
    listVersions(query: Dto.FileQuery): Observable<ApiResult<Dto.FileDescription[]>> {
        return this._phttp.post(`/platform/admin/list`,query||{}).pipe(map((ret:Response) => <ApiResult<Dto.FileDescription[]>>({  data: ret.json() , originalResponse: ret })));
    }
    reupload(input: Dto.ReuploadRequest): Observable<ApiResult<Dto.IdResponse>> {
        return this._phttp.post(`/platform/admin/reupload`,input||{}).pipe(map((ret:Response) => <ApiResult<Dto.IdResponse>>({  data: ret.json() , originalResponse: ret })));
    }

}
ajaxproxy类

这是使用的ProxyHttp类

import { Injectable } from '@angular/core';
import * as ngHttp from '@angular/http';
import { of, Observable, throwError } from 'rxjs';
import { map, catchError} from 'rxjs/operators'; 

import { UserService } from '../services/user.service';
import { BinaryTransferObject } from './binary-transfer-object';
import * as Dto from './dtos';
import { ProxyProxy } from './generated/ProxyController';

@Injectable()
export class ProxyHttp {

    constructor(private _proxy: ProxyProxy, private _userService: UserService) { }

    get(url: string, _options?: ngHttp.RequestOptionsArgs): Observable<ngHttp.Response> { return this.makeRequest({ data: null, url: url, verb: 'get' }); }

    post(url: string, body: any, _options?: ngHttp.RequestOptionsArgs): Observable<ngHttp.Response> {
        if (body instanceof ArrayBuffer) {
            return this.binaryRequest({ binary: body, url: url });
        }
        if (body instanceof BinaryTransferObject) {
            return this.binaryRequest({ binary: body.serialize(), url: url });
        } else return this.makeRequest({ data: body && JSON.stringify(body), url: url, verb: 'post', });
    }
    put(url: string, body: any, _options?: ngHttp.RequestOptionsArgs): Observable<ngHttp.Response> { return this.makeRequest({ data: body && JSON.stringify(body), url: url, verb: 'put', }); }
    delete(url: string, _options?: ngHttp.RequestOptionsArgs): Observable<ngHttp.Response> { return this.makeRequest({ data: null, url: url, verb: 'delete', }); }
   upload(url: string, body: any) {
        this._userService.touched.next(null); // touch
        const stream = this._proxy.fileUpload(url, body).pipe(map(ret => ret.originalResponse) , catchError((error: any) => {
            if (error.status === 401) {
                this._userService.unauthorized();
                return of(null);
            } else {
                return throwError(error);
            }
        }));
        return stream;
    }

    private makeRequest(request: Dto.ProxyRequest): Observable<ngHttp.Response> {
        this._userService.touched.next(null); // touch
        const stream = this._proxy.proxy(request).pipe(map((ret => ret.originalResponse) ,catchError((error: any) => {
            if (error.status === 401) {
                this._userService.unauthorized();
                return of(null);
            } else {
                return throwError(error);
            }
        })));
        return stream;
    }

    private binaryRequest(body: { binary: ArrayBuffer, url: string }): Observable<ngHttp.Response> {
        this._userService.touched.next(null); // touch
        const stream = this._proxy.binaryUpload(<any>body.binary, body.url).pipe(map((ret => ret.originalResponse), catchError((error: any) => {
            if (error.status === 401) {
                this._userService.unauthorized();
                return of(null);
            } else {
                return throwError(error);
            }
        })));
        return stream;
    }
}
从'@angular/core'导入{Injectable};
从“@angular/http”导入*作为ngHttp;
从“rxjs”导入{of,observatable,throwerr};
从“rxjs/operators”导入{map,catchError};
从“../services/user.service”导入{UserService};
从“./二进制传输对象”导入{BinaryTransferObject};
将*作为Dto从“/Dto”导入;
从“./generated/ProxyController”导入{ProxyProxy};
@可注射()
导出类ProxyHttp{
构造函数(private _proxy:ProxyProxy,private _userService:userService){}
get(url:string,_options?:ngHttp.RequestOptionsArgs):可观察的{返回this.makeRequest({data:null,url:url,动词'get'});}
post(url:string,body:any,_options?:ngHttp.RequestOptionsArgs):可观察{
if(阵列缓冲器的主体实例){
返回此.binaryRequest({binary:body,url:url});
}
if(BinaryTransferObject的主体实例){
返回此.binaryRequest({binary:body.serialize(),url:url});
}否则返回this.makeRequest({data:body&&JSON.stringify(body),url:url,动词'post',});
}
put(url:string,body:any,_options?:ngHttp.RequestOptionsArgs):可观察的{返回this.makeRequest({data:body&&JSON.stringify(body),url:url,动词:'put',});}
delete(url:string,_options?:ngHttp.RequestOptionsArgs):可观察的{返回this.makeRequest({data:null,url:url,动词:'delete',});}
上传(url:string,body:any){
this._userService.touch.next(null);//touch
const stream=this.\u proxy.fileUpload(url,body).pipe(map(ret=>ret.originalResponse),catchError((error:any)=>{
如果(error.status==401){
此._userService.unauthorized();
返回(空);
}否则{
返回投掷器(错误);
}
}));
回流;
}
私有makeRequest(请求:Dto.ProxyRequest):可观察{
this._userService.touch.next(null);//touch
const stream=this.\u proxy.proxy(request).pipe(map((ret=>ret.originalResponse)),catchError((error:any)=>{
如果(error.status==401){
此._userService.unauthorized();
返回(空);
}否则{
返回投掷器(错误);
}
})));
回流;
}
私有binaryRequest(主体:{binary:ArrayBuffer,url:string}):可观察{
this._userService.touch.next(null);//touch
const stream=this.\u proxy.binaryUpload(body.binary,body.url).pipe(map((ret=>ret.originalResponse)),catchError((error:any)=>{
如果(error.status==401){
此._userService.unauthorized();
返回(空);
}否则{
返回投掷器(错误);
}
})));
回流;
}
}

这就是您应该如何重写您的
LocalizationProxy
(以及所有
HttpClient
相关代码)

请参见添加的
{observe:'response'}
作为http调用选项(而不是
post/put/patch
,它是第三个参数,用于获取第二个参数),我只使用已经包含JS对象的
res.json()
,而不是调用
res.body

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

@Injectable()
export class LocalisationProxy {

  constructor(private _nghttp: HttpClient, private _phttp: ProxyHttp) {}

  getAllCultures(): Observable<ApiResult<Dto.Culture[]>> {
    return this._nghttp.get('/platform/localisation/cultures/all', { observe: 'response' })
      .pipe(map((res: HttpResponse<any>) => ({ data: res.body, originalResponse: res })));
  }
}
从'@angular/common/http'导入{HttpClient,HttpResponse};
从“@angular/core”导入{Injectable};
从“rxjs”导入{Observable};
从“rxjs/operators”导入{map};
@可注射()
导出类LocalizationProxy{
构造函数(private _nghttp:HttpClient,private _phttp:ProxyHttp){}
getAllCultures():可观察{
返回此内容。nghttp.get('/platform/localization/cultures/all',{observe:'response'})
.pipe(map((res:HttpResponse)=>({data:res.body,originalResponse:res}));
}
}

什么是
ProxyHttp
?如果它在引擎盖下使用
HttpClient
发出http请求,那么响应上就没有
.json()
方法-您将立即得到它。如果我删除json方法,我将得到以下错误类型“{data:response;originalResponse:response;}”无法转换为类型“apireult”。属性“数据”的类型不兼容。类型“Response”与类型“IdResponse”不可比较。能否在此处发布
ProxyHttp
实现?这是你的服务吗?我已经发布了代码我还有一个方法使用httpClient@Injectable()导出类ProxyProxy{constructor(private{nghttp:httpClient){}authenticateWindowsAuth():Observable{返回这个({data:ret.json(),originalResponse:ret});}我收到消息Generic Type HttpResponse需要一个类型参数只需将
map
参数类型替换为
HttpResponse
我确实尝试过,并得到了类型为“OperatorFunction”的错误参数是的,这是因为错误的
getAllCultures
返回类型-您正在返回带有
数据的对象>originalResponse
键。但您提供的代码中已经存在错误。IDK是您确切期望的,但出于调试目的,您可以使用
getAllCultures():Observable
以使其工作。原始代码中的getAllcultures返回了数据和原始响应键,这是预期的。升级之前它工作正常。唯一的区别是调用了ret.json()。我需要做的就是删除ret.json()并使其工作
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable()
export class LocalisationProxy {

  constructor(private _nghttp: HttpClient, private _phttp: ProxyHttp) {}

  getAllCultures(): Observable<ApiResult<Dto.Culture[]>> {
    return this._nghttp.get('/platform/localisation/cultures/all', { observe: 'response' })
      .pipe(map((res: HttpResponse<any>) => ({ data: res.body, originalResponse: res })));
  }
}