映射到JSON一个HTTP响应,然后映射JSON到模型问题(Issues mapping a HTT

2019-09-29 08:03发布

我试图映射一个HTTP响应JSON,然后映射该JSON的一部分,一个单接口,因为它有我没有在我的单界面需要很多的值。 我的应用程序编译没有任何问题,但是当我测试REST功能我得到以下运行时错误,我知道这个问题是不是与后端为我能成功控制台登录的响应。 任何想法,我做错了什么?

My error:

    vendor.bundle.js:8137 Angular is running in the development mode. Call enableProdMode() to enable the production mode.
    main.bundle.js:553 TypeError: res.json(...).map is not a function
        at MapSubscriber.project (main.bundle.js:1330)
        at MapSubscriber._next (vendor.bundle.js:42853)
        at MapSubscriber.Subscriber.next (vendor.bundle.js:4709)
        at XMLHttpRequest.onLoad (vendor.bundle.js:47289)
        at ZoneDelegate.webpackJsonp.749.ZoneDelegate.invokeTask (polyfills.bundle.js:2478)
        at Object.onInvokeTask (vendor.bundle.js:9228)
        at ZoneDelegate.webpackJsonp.749.ZoneDelegate.invokeTask (polyfills.bundle.js:2477)
        at Zone.webpackJsonp.749.Zone.runTask (polyfills.bundle.js:2245)
        at XMLHttpRequest.ZoneTask.invoke (polyfills.bundle.js:2540)


My code:

    retrieveTicket(barcode: string) : Observable<any> {
          return this.http.get(`${this.API_URL}POS/RetrieveTicket/${barcode}`, this.options)
          .map((res: Response) => res.json().map(ticket => {          
                Object.assign({
                    ResponseCode: ticket.ResponseCode,
                    CustomError: ticket.CustomError,
                    ticketDate: ticket.POSTicket.Date,
                    ticketTime: ticket.POSTicket.EndTime,
                    cashierName: ticket.POSBarCode.POSCashier_Name,
                    tranNo: ticket.POSTicket.TranNo,
                    tranValue: ticket.POSTicket.ScanValue,
                    securityChecked: ticket.POSBarCode.SecurityChecked
                }) as ITicket})         
          )
          .catch((error: any) => Observable.throw(error || 'server error'));
      } 


 my interface:

        import { BaseRequestInterface } from './base-request.interface';

        export interface ITicket extends IBaseRequest {
            ticketDate: string;
            ticketTime: string;
            cashierName: string;
            tranNo: string;
            tranValue: string;
            timeSincePurchase: string;
            securityChecked: boolean;

            export function setTicket(obj?: any) {
                super();
                this.ResponseCode = obj && obj.ResponseCode || null;
                this.CustomError = obj && obj.CustomError || null;
                this.ticketDate = obj && obj.ticketDate || null;
                this.ticketTime = obj && obj.ticketTime || null;
                this.cashierName = obj && obj.cashierName || null;          
                this.tranNo = obj && obj.tranNo || null;
                this.tranValue = obj && obj.tranValue || null;
                this.timeSincePurchase = obj && obj.timeSincePurchase || null;
                this.securityChecked = obj && obj.securityChecked || null;
            }


        }

my BaseRequest Interface:

//  Base request class that returns from BRMService API
export interface IBaseRequest {
    //  Public properties available
     BaseURI?: string;
     CustomError?: string;
     ProviderName?: string;
     RequestFormData?: string;
     RequestURI?: string;
     ResponseCode?: number;    
}

Answer 1:

编辑:

我们了解到,我们正在处理的只是一个对象,而不是对象的数组,所以映射对象类型TicketModel想做到以下(缩短):

retrieveTicket(barcode: string) {
   return this.http.get(...)
     .map(res => res.json())
     .map(res => ({ResponseCode:res.ResponseCode, CustomError:res.CustomError}) as TicketModel)
}

接口:

export interface TicketModel {
  ResponseCode: number; 
  CustomError:string 
}

DEMO


原帖:

目前还不清楚,如果你正在使用你的类或接口TicketModel ,反正我建议你使用的接口;),然后你就可以简单地映射传入数据,如(缩短的版本):

.map((res:Response) => res.json().map(x => 
   Object.assign({ResponseCode: x.ResponseCode, 
                  CustomError:x.CustomError}) as TicketModel)))

如果你的反应是用含有内部数组中对象items ,只需添加items中:

.... res.json().items.map( ...


文章来源: Issues mapping a HTTP response to a JSON and then mapping the JSON to a model
标签: rest angular