在 Angular 中发送消息“Object Object”

Message "Object Object" in Angular

提问人:mohciniya 提问时间:4/24/2022 最后编辑:mohciniya 更新时间:4/24/2022 访问量:111

问:

事实上,我希望在表单的输入中显示 solde 的值。

图片 1

我遇到了错误,我不明白问题?[Object Object]

我认为问题出在这里?我不知道如何检索变量?SOLDE

private getSolde(): void {
    this.service.getSolde(this.svm!).pipe(
      takeUntil(this.unsubscribe$)
    ).subscribe(res => {
      if (res.RETURNCODE === ApiResponseCodeEnum.Ok) {
        this.details = res.TRANS;
        this.qtte = res.TRANS;  // solde ?????
        console.log("Bonjour")
        console.log(this.qtte)
      }
    });
  }

内部-transfert-watch.response.ts

export interface InternalTransfertWatchResponse extends ApiResponse {
    TRANS: AdvTitres[];
  }
 
  export interface AdvTitres {
 
      TITRE: {
          LABEL: string,
          STOCK: string,
          ISIN: string,
          SVM: number,
      },
      SOLDE: number,
      COUPON: number,
      QTE_VENTE: number,
      QTE_BLOQ: number,
      QTE_TRF: number,
 
  }

这是代码 TS 和 HTML

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

  details?: AdvTitres[] = [];
 
 
  svm: string | null = null;
  qte: number;
  type: string = '';
  dest: string = '';
  qtte: AdvTitres[];


  constructor(   
    private service: InternalTransfertWatchService,
    private activatedRoute: ActivatedRoute,
    private location: Location,
  ) { }

  ngOnInit(): void {
    this.svm = this.activatedRoute.snapshot.paramMap.get('svm');
  
    if (!this.svm) {
      this.goBack();
      return;
    }
    this.getSolde();
  }

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


  private getSolde(): void {
    this.service.getSolde(this.svm!).pipe(
      takeUntil(this.unsubscribe$)
    ).subscribe(res => {
      if (res.RETURNCODE === ApiResponseCodeEnum.Ok) {
        this.details = res.TRANS;
        this.qtte = res.TRANS;  // solde
        console.log("Hello")
        console.log(this.qtte)
      }
    });
  }

  /* Form */ 
  submit(): void {
    this.service.getInternalTransfertStock(parseInt(this.svm!), this.qte, this.type, this.dest).pipe(
      takeUntil(this.unsubscribe$)
    ).subscribe(res => {
      if (res.RETURNCODE === ApiResponseCodeEnum.Ok) {
        this.goBack();
      }
    });
  }


  goBack(): void {
    this.location.back();
  }


}

&&

<div class="container" *ngIf="details">
  <table class="table table-hover table-striped spaceLeft">
    <tbody>
      <tr>
        <th>SVM</th>
        <td>{{details[0].TITRE.SVM}}</td>
        <th>Solde</th>
        <td>{{details[0].SOLDE}}</td>
        <!-- <td>{{details|json}}</td> -->
      </tr>
    </tbody>
  </table>

  <div class="card" style="width: 100%;">
    <div class="card-body">
      <form #formulaire="ngForm" (ngSubmit)="formulaire.form.valid && submit()">
        <div class="row row-cols-3 pt-3">
          <div class="col text-end">
            <label for="qte" class="form-label">Quantity</label>
          </div>
          <div class="col-4">
            <input
              id="qte"
              name="qte"
              type="text"
              class="form-control"
              style="min-width: 380px"
              maxlength="25"
              [(ngModel)]="qtte"
            />
          </div>
        </div>

        <div class="row row-cols-3 pt-3">
          <div class="col text-end">
            <label for="type" class="form-label">Beneficiary change</label>
          </div>
          <div class="col-4">
            <select [(ngModel)]="type" name="type" class="form-select">
              <option value="O">O</option>
              <option value="">N</option>
            </select>
          </div>
        </div>
      </form>
    </div>
  </div>
</div>

这里也是文件 JSON

如果你有想法,我很感兴趣。

非常感谢。

编辑

private getSolde(): void {
    this.service.getSolde(this.svm!).pipe(
      takeUntil(this.unsubscribe$)
    ).subscribe(res => {
      if (res.RETURNCODE === ApiResponseCodeEnum.Ok) {
        this.details = res.TRANS;
        console.log("Test 1")
        this.qtte = res[0]["SOLDE"];
      }
    });
  }

评论


答:

0赞 H0-pe 4/24/2022 #1

看来你在这里混淆了什么。请记住,这是一个数组,没有一个确切的值,而是有几个。TRANSSOLDE

如果你能提供更详细的信息,你到底想用这些值做什么,无论你是想在列表中显示它们,还是如果你想显示所有数组项,我们可以尝试找出一个解决方案。SOLDETRANS

评论

0赞 mohciniya 4/24/2022
我想检索 SOLDE 值并将其添加到表单输入中。我试过这个: this.qtte = res[0][“SOLDE”];但我有一个错误消息,我编辑了我的第一篇文章。我不知道我的问题是否可以理解?ERROR TypeError: Cannot read properties of undefined (reading 'SOLDE')
0赞 H0-pe 4/24/2022
尝试res.TRANS[0]["SOLDE"]