首页 > 解决方案 > 使用角度服务更新多行的正确方法

问题描述

试图了解这应该如何工作。我正在使用弹簧微服务设置,这个角度服务调用更新了一条记录:

角服务:

save(recordModel) {
    recordModel.lastUpdated = new Date();
    recordModel.whoUpdated = this.loginService.getUserId();
    this.dataService.httpPut(recordModel._links.self.href, recordModel).subscribe(resp => { },
      (resp) => {
      this.logger.error("Save Failed", resp);
      this.dialogError(resp);
    });
  }

我需要能够更新数据库中所有记录的某些 json 属性:

updateAttributeIds(interfaceName: string) {
    this.logger.info("Updating Attribute Id's");
    let syncIssues = 0;
    for (let i = 0; i < this.dataSource.length; i++) {
      if(this.dataSource[i].attributes.attributeId != this.dataSource[i].id) {
        syncIssues ++;
        if(this.dataSource[i].attributes.interfaceName === interfaceName) {
        this.dataSource[i].attributes.attributeId = this.dataSource[i].id;
          this.logger.info("Updating", this.dataSource[i].id);
          this.save(this.dataSource[i]);
        }
      }
    }
    this.logger.warn("Updating Attribute Id's", syncIssues);
  }

这似乎有效,但是 this.save(this.dataSource[i]); 不等待承诺,如果需要,我会尝试在它尝试更新下一条记录之前弄清楚它。感觉就像在一个大型数据集上,最终我会耗尽资源 - 或线程?只是不清楚到底发生了什么。

谢谢

标签: angular

解决方案


推荐阅读