角无限循环以更新Firestore中的数据

问题描述

我是新手。当我提交表单时,它将调用onBuyStock()将输入值插入BuyHistory集合中,然后我需要更新OverviewStock集合中的值。因为我需要从OverviewStock集合中获取当前值并执行增量操作,所以这里发生了问题。我打电话给this.stockService.GetOverviewStock()并订阅它。当我想使用response[0].Quantity + 1方法中的subscribe()更新值时,将导致无限循环来更新文档。

任何解决方案?

html

<mat-card class="mat-elevation-z8">
    <mat-card-header>
        <mat-card-title>Buy Stock</mat-card-title>
    </mat-card-header>

    <mat-card-content>
        <form [formGroup]="form" (submit)="onBuyStock()" #formDirective="ngForm">

            <div>
                <mat-form-field class="example-full-width">
                    <input matInput type="text" placeholder="Stock Name" aria-label="Number" formControlName="StockName"
                        [matAutocomplete]="auto">
                    <mat-autocomplete #auto="matAutocomplete">
                        <mat-option *ngFor="let option of stockNameOptions" [value]="option">
                            {{option}}
                        </mat-option>
                    </mat-autocomplete>
                </mat-form-field>
            </div>

            <div>
                <mat-form-field>
                    <input matInput type="number" placeholder="Price" aria-label="Number" formControlName="Price"
                        min="0" (keyup)="onCalculateNetBuy()">
                </mat-form-field>
            </div>

            <div>
                <mat-form-field>
                    <input matInput type="number" placeholder="Qty" aria-label="Number" formControlName="Quantity"
                        min="1" (keyup)="onCalculateNetBuy()">
                </mat-form-field>
            </div>

            <button mat-raised-button color="buy" type="submit" fxFlexFill>Buy</button>
        </form>
    </mat-card-content>
</mat-card>

component.ts

....
...

onBuyStock() {
    if (this.form.invalid) {
      return;
    }

    this.InsertDataIntoBuyHistory({
      StockName: this.form.value.StockName,Quantity: this.form.value.Quantity,UnitPrice: this.form.value.Price,BuyDate: new Date(),Brokerage: this.brokerage,Tax: this.tax,StampDuty: this.stampDuty,ClearingFee: this.clearingFee,TotalExtraCost: this.totalExtraCost,TotalBuyCost: this.netBuy,});

    this.testName = this.form.value.StockName
    this.stockService.GetOverviewStock().subscribe((response) => {
      response = response.filter((item) => {
        return item.StockName == this.testName;
      });
      this.firestore
        .collection("OverviewStock")
        .doc(this.testName)
        .set(
          {
            StockName: response[0].StockName,Quantity: response[0].Quantity + 1,TotalMoneySpent: this.testPrice,},{ merge: true }
        );
    });

    this.form.reset();
    this.formDirective.resetForm();
}

  private InsertDataIntoBuyHistory(data: BuyHistory) {
    this.firestore.collection("BuyHistory").add(data);
  }

stockService.ts

  GetOverviewStock() {
    return this.firestore
      .collection<StockModel>("OverviewStock",(ref) => ref.orderBy("StockName","asc"))
      .valueChanges();
  }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)