Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用于更新firestore中数据的角度无限循环_Javascript_Angular_Google Cloud Firestore_Angularfire - Fatal编程技术网

Javascript 用于更新firestore中数据的角度无限循环

Javascript 用于更新firestore中数据的角度无限循环,javascript,angular,google-cloud-firestore,angularfire,Javascript,Angular,Google Cloud Firestore,Angularfire,我是个新手。当我提交表单时,它将调用onBuyStock()将输入值插入buyshistory集合,然后我需要更新概览股票集合中的值。问题发生在这里,因为我需要从OverviewStock集合中获取当前值并进行增量。我调用this.stockService.GetOverviewStock()并订阅它。当我想使用subscribe()方法中的response[0].Quantity+1更新值时,它将导致无限循环来更新文档 有什么解决办法吗 html 斯托克服务公司 GetOverviewSt

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

有什么解决办法吗

html

斯托克服务公司

  GetOverviewStock() {
    return this.firestore
      .collection<StockModel>("OverviewStock", (ref) => ref.orderBy("StockName", "asc"))
      .valueChanges();
  }
GetOverviewStock(){
把这个还给我
.collection(“概览股票”(ref)=>ref.orderBy(“股票名称”、“asc”))
.valueChanges();
}

Hi@Jayson您是否尝试过在变量中设置值并发送而不是
Quantity:response[0]。Quantity+1
?这应该可以帮助你避免无休止的循环。如果你试过了,那就没用了。仍然导致无穷大loopHi@Jayson您是否尝试过部分修改您的方法以找出问题所在?
subscribe()
中的任何特定部分似乎都不会导致循环有限。
....
...

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);
  }
  GetOverviewStock() {
    return this.firestore
      .collection<StockModel>("OverviewStock", (ref) => ref.orderBy("StockName", "asc"))
      .valueChanges();
  }