首页 > 解决方案 > 错误类型错误:this.homeboxsp.map 不是 patchForm 中的函数

问题描述

我的代码中有这个patchForm:

populateForm() {
    this.hsP.homeboxPGetAll().subscribe(
      homeboxsp => {
        this.homeboxsp = homeboxsp
        let controls = {
          'homeboxpackage_id': new FormControl('', Validators.required)
        };
        for (let i = 0; i < this.homeboxsp.length; i++) {
          controls['active-' + this.homeboxsp[i].homeboxpackage_id] = new FormControl(this.homeboxsp[i].active === 1, Validators.required)
        }
        this.activeHomeboxPForm = new FormGroup(controls);
        this.patchForm();
      }
    )
  }
      patchForm() {
        this.activeHomeboxPForm.patchValue({
          homeboxpackage_id: this.homeboxsp.map(x => x.homeboxpackage_id),
        });
      }

服务.ta

public homeboxPGetAll(): Observable<HomeboxP[]> {
    let headers = new Headers();
    headers.append('x-access-token', this.auth.getCurrentUser().token);
    return this.http.get(Api.getUrl(Api.URLS.homeboxPGetAll), {
      headers: headers
    })
      .map((response: Response) => {
        let res = response.json();
        if (res.StatusCode === 1) {
          this.auth.logout();
        } else if (res.StatusCode === 4) {
          return false;
        } else if (res.StatusDescription === 'No result') {
          return false;
        } else {
          return res.StatusDescription.map(homeboxP => {
            return new HomeboxP(homeboxP);
          });
        }
      });
  }

当 API 获取数据时,不会显示此错误。当 API 在控制台中获取空数据时显示此错误:

错误类型错误:this.homeboxsp.map 不是 HomeboxComponent.patchForm 中的函数

你能问我任何想法,如何解决这个错误?

标签: angulartypescriptpatch

解决方案


推荐阅读