我的代码中有这个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),
});
}
Service.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在控制台中获取空数据时,显示此错误:
错误TypeError:this.homeboxsp.map不是一个函数 在HomeboxComponent.patchForm
请问我有什么主意,如何解决此错误?