core.mjs:6484 错误 TypeError:无法读取未定义的属性(读取“QTY”)

core.mjs:6484 ERROR TypeError: Cannot read properties of undefined (reading 'QTY')

提问人:mohciniya 提问时间:2/11/2022 最后编辑:mohciniya 更新时间:2/11/2022 访问量:668

问:

我在控制台中有一条错误消息,但我不明白问题出在哪里。

安慰

股票观察.ts

export interface StockWatch {
    svm: number;
    ticker: string;
    name: string;
    buy: {
        qty: number;
        limit: number;
    },
    volume: number;
    last: number;
}

股票观察.response.ts

import { ApiResponse } from "src/app/shared/types/api.response";


export interface StockWatchResponse extends ApiResponse {
    STOCKWATCH: {
        ELEMENT: {
            CODE: string;
            ISIN: string;
            SVM: number;
            LABEL: string;
            CURRENCY: string;
            REALTIME: string;
            LASTPRICE: number;
            DATELASTPRICE: string;
            TIMELASTPRICE: string;
            PLACE: number;
            PLACELABEL: string;
            ALERTE: string;
            PRICEVARIATIONPRC: number;
            PRICEVARIATIONNOM: number;
            PRICEVARIATIONCUR: string;
            VOLUME: number;
            ACHTEUR1: AchtVend;
            VENDEUR1: AchtVend;
        }[];
    }
}

export interface AchtVend {
    NUMBRE: number;
    QTY: number;
    COURS: number;
}

股票观察.service.ts

@Injectable()
export class StockWatchService {
  private readonly api: string = environment.api;

  constructor(private http: HttpClient, private store: Store) { }

  getStockWatch(showBusy: boolean): Observable<StockWatchResponse> {
    let headers = new HttpHeaders();
    headers = headers.set('AddCustomer', '');
    if (!showBusy) {
      headers = headers.set('DoNotSetBusy', '');
    }

    return this.http.post<StockWatchResponse>(this.api + `/AZERTY`, {
      ACTION: "DSP",
      SVM: 0
    }, { headers });
  }
}

股票观察.component.ts

我认为问题出在方法上?getStockWatch

export class StockWatchComponent implements OnInit, OnDestroy {
  private unsubscribe$ = new Subject<void>();

  lines: StockWatch[] = [];
  gotResponse: boolean = false;

  private unsubscribeTimer$ = new Subject<void>();
  timer: number = -1;

  constructor(private service: StockWatchService) { }

  ngOnInit(): void {
    this.getStockWatch(true);
    this.startTimerStock();
  }

  ngOnDestroy(): void {
    this.resetSubscriptions(false);
    this.unsubscribe$.next();
    this.unsubscribe$.complete();
  }

  refresh(): void {
    this.resetSubscriptions();
    this.getStockWatch(false);
    this.startTimerStock();
  }

  private resetSubscriptions(recreate: boolean = true): void {
    this.unsubscribeTimer$.next();
    this.unsubscribeTimer$.complete();
    if (recreate) {
      this.unsubscribeTimer$ = new Subject<void>();
    }
    this.timer = -1;
  }

  private getStockWatch(showBusy: boolean): void {
    this.gotResponse = false;
    this.service.getStockWatch(showBusy).pipe(
      finalize(() => this.timer = 30),
      takeUntil(this.unsubscribe$)
    ).subscribe(res => {
      this.gotResponse = true;
      this.lines = res.STOCKWATCH.ELEMENT.map(x => {
        return {
          svm: x.SVM,
          ticker: x.CODE,
          name: x.LABEL,
          buy: {
            qty: x.ACHTEUR1.QTY,
            limit: x.ACHTEUR1.COURS
          },
          volume: x.VOLUME,
          last: x.LASTPRICE
        }
      });
      
    });
  }

  private startTimerStock(): void {
    timer(0, 1000).pipe(
      filter(() => this.timer >= 0),
      takeUntil(this.unsubscribeTimer$)
    ).subscribe(() => {
      this.timer -= 1;
      if (this.timer < 0) {
        this.getStockWatch(false);
      }
    });
  }


}

编辑

在此处输入图像描述

评论

0赞 Misha Mashina 2/11/2022
你能分享返回的x的日志吗?this.lines = res.STOCKWATCH.ELEMENT.map(x => {...
0赞 mohciniya 2/11/2022
@Misha Mashina:当然,我只是编辑了我的帖子
0赞 Misha Mashina 2/11/2022
您的响应没有 和 键,因此 成为 undefined 的属性。ACHTEUR1VENDEUR1QTY
1赞 mohciniya 2/12/2022
@Misha Mashina:确实如此!事实上,问题出在后端。

答: 暂无答案