在只读循环中修改了Angular / Typescrit对象?

时间:2019-01-22 08:15:44

标签: javascript angular typescript

在对象数组中搜索具有给定属性的对象时遇到麻烦。数据源最初是REST-API,但是由于数据仅不时更改,因此将其保存在对象中,该对象用于应用程序中的其他请求。
这是引起问题的代码:

  constructor(private route: ActivatedRoute, private poolService: PoolDTOServiceService) {
    this.route.params.subscribe((params: Params) => this.getPool(params));
  }

  ngOnInit() {
  }

  getPool(params: Params) {
    console.log('called method');
    for (let i = 0; i < this.poolService.jsonResponse.pools.length; i++) {
      const pool = this.poolService.jsonResponse.pools[i];
      for (let o = 0; o < pool.membersReference.items.length; o++) {
        const member = pool.membersReference.items[o];
        if (member.name === params['name']) {
          console.log('called if');
          console.log(member);
          this.pool = member;
          break;
        }
      }
    }
  }

以下是循环内访问的对象(jsonResponse只是包含池列表的虚拟对象):

这里是PoolDTO(由pools[i]访问)。这是一个简单的DTO:

  private _kind: String;
  private _name: String;
  private _partition: String;
  private _fullPath: String;
  private _generation: String;
  private _selfLink: String;
  private _allowNat: String;
  private _allowSnat: String;
  private _description: String;
  private _ignorePersistanceWeight: String;
  private _ipTosToClient: String;
  private _ipTosToServer: String;
  private _linkQosToClient: String;
  private _LinkQosToServer: String;
  private _loadBalancingMode: String;
  private _minActiveMembers: String;
  private _minUpMembers: String;
  private _minUpMembersAction: String;
  private _minUpMembersChecking: String;
  private _monitor: String;
  private _queueDepthLimit: String;
  private _queueOnConnectionLimit: String;
  private _queueTimeLimit: String;
  private _reselectTries: String;
  private _serviceDownAction: String;
  private _slowRampTime: String;
  private _membersReference: MembersReferenceDTO;

membersReference拥有一个名为items的数组,其中包含PoolMemberDTO s:

  private _dynamicRatio: String;
  private _ephermal: String;
  private _inheritProfile: String;
  private _logging: String;
  private _priorityGroup: String;
  private _rateLimit: String;
  private _ratio: String;
  private _state: String;
  private _address: String;
  private _subItems: any[];
  private _name: String;

这里是poolService

private _jsonResponse: JsonResponseDTO;
  constructor(private http: HttpClient) {
    this.getPools().subscribe((p) => {
      this._jsonResponse = p;
    });
  }

  public getPools(): Observable<JsonResponseDTO> {
    return this.http.get<JsonResponseDTO>('http://localhost:8080/pools');
  }

  public getPoolArray(): PoolDTO[] {
    return this._jsonResponse.pools;
  }

  public setJsonResponse() {
    this.http.get<JsonResponseDTO>('http://localhost:8080/pools').subscribe((next) => {this._jsonResponse = next; });
  }


  get jsonResponse(): JsonResponseDTO {
    return this._jsonResponse;
  }

setJsonResponse方法仅用于不时获取新数据。

现在描述问题:引起问题的组件由子路由“创建”,并提供了name属性。此对象用于查找池中的特定池成员。我记录了该成员,然后发生了什么: 生成了两个日志:
第一个是正确的,第二个包含不同的ratio值。其他属性相等。

我到目前为止所做的事情
我在forEach()和传统的for循环之间切换,在if条件中甚至使用return / break离开循环并获得“第一个”。那也没有用。

我检查的内容
我在后端检查了单元测试,发现没有dupilcate服务器。名称属性用于比较,因此这里的行为是不可能的。

从后端接收到jsonResponse对象之后,我记录了该对象,并检查其中相应的池是否正确。是的。

有人可以在这里帮助我吗?

0 个答案:

没有答案