错误 TS2339:类型 'DataModel[]' 上不存在属性'ITEMS'



我得到此错误消息:TS2339:属性'项目'在类型'datamodel []'

上不存在

我在离子/角工作,并使用模型进行反应性形式,并从http中获取数据。

有模型(model.ts(:

export class DataModel {
    public ITEMS: any[];
    constructor(public NAME: string, public QUANTITY: Array<string>) {
        this.ITEMS = [{NAME, QUANTITY}];
    }
}

有代码部分来自form.page.ts

的错误部分
  setItems() {
    const control = this.itemsForm.controls.items as FormArray;
    this.dbData.ITEMS.forEach(element => {
      control.push(
        this.formBuilder.group({
          name: element.NAME,
          quantity: this.setItem(element)
        })
      );
    });
  }

有完整的代码表单.page.ts

export class Form2Page implements OnInit, OnDestroy {
  obsSubscription: Subscription;
  itemsForm: FormGroup;
  public dbData: DataModel[];

  constructor(
    private formBuilder: FormBuilder,
    private storageService: FormStorageService
  ) {}
  ngOnInit() {
    // Get data from DB
    this.storageService.getDBData();
    this.obsSubscription = this.storageService.dbSubject.subscribe(
      (value: any[]) => {
        this.dbData = value;
    // Creating the FORM
        this.initForm();
        this.setItems();
      },
      error => {
        console.log('erreur');
        console.log(error);
      }
    );
  }
  initForm() {
    this.itemsForm = new FormGroup({
      items: new FormArray([])
    });
  }
  setItems() {
    const control = this.itemsForm.controls.items as FormArray;
    this.dbData.ITEMS.forEach(element => {
      control.push(
        this.formBuilder.group({
          name: element.NAME,
          quantity: this.setItem(element)
        })
      );
    });
  }
  setItem(value) {
    const array = new FormArray([]);
    value.QUANTITY.forEach(element => {
      array.push(this.formBuilder.control(element));
    });
    return array;
  }
  findFieldArray(field: any): FormArray {
    return this.itemsForm.get(field) as FormArray;
  }
  addNewItems() {
    const control = this.itemsForm.controls.items as FormArray;
    control.push(
      this.formBuilder.group({
        name: '',
        quantity: this.formBuilder.array([this.formBuilder.control('')])
      })
    );
  }
  addNewItem(item: FormGroup) {
    (item.get('quantity') as FormArray).push(this.formBuilder.control(''));
  }
  removeItem(item: FormGroup, qtyIndex: number, itemIndex: number) {
    (item.get('quantity') as FormArray).removeAt(qtyIndex);
  }
  removeItems(itemIndex: number) {
    const form = this.itemsForm.get('items') as FormArray;
    form.removeAt(itemIndex);
  }
  checkArray(itemIndex: number) {
        // Get the quantity array lenght to display the remove ITEMS button
        return ((this.itemsForm.get('items') as FormArray)
        .at(itemIndex)
        .get('quantity') as FormArray).length;
  }
  onFormSubmit() {
    console.log('Submit : ', this.itemsForm.value);
  }
  ngOnDestroy() {
    this.obsSubscription.unsubscribe();
  }
}

感谢您的帮助

由于您将public dbData: DataModel[]定义为一个数组,因此应该将其视为一个。

我的猜测是您打算将其定义为public dbData: DataModel,然后在您调用this.dbData.ITEMS.forEach(element => {

时可以工作

这是完整的更改:

export class Form2Page implements OnInit, OnDestroy {
  obsSubscription: Subscription;
  itemsForm: FormGroup;
  public dbData: DataModel;  // #1 change

  constructor(
    private formBuilder: FormBuilder,
    private storageService: FormStorageService
  ) {}
  ngOnInit() {
    // Get data from DB
    this.storageService.getDBData();
    this.obsSubscription = this.storageService.dbSubject.subscribe(
      (value: any[]) => {
        this.dbData = { ITEMS: value };  // #2 change
    // Creating the FORM
        this.initForm();
        this.setItems();
      },
      error => {
        console.log('erreur');
        console.log(error);
      }
    );
  }
  initForm() {
    this.itemsForm = new FormGroup({
      items: new FormArray([])
    });
  }
  setItems() {
    const control = this.itemsForm.controls.items as FormArray;
    this.dbData.ITEMS.forEach(element => {
      control.push(
        this.formBuilder.group({
          name: element.NAME,
          quantity: this.setItem(element)
        })
      );
    });
  }
  setItem(value) {
    const array = new FormArray([]);
    value.QUANTITY.forEach(element => {
      array.push(this.formBuilder.control(element));
    });
    return array;
  }
  findFieldArray(field: any): FormArray {
    return this.itemsForm.get(field) as FormArray;
  }
  addNewItems() {
    const control = this.itemsForm.controls.items as FormArray;
    control.push(
      this.formBuilder.group({
        name: '',
        quantity: this.formBuilder.array([this.formBuilder.control('')])
      })
    );
  }
  addNewItem(item: FormGroup) {
    (item.get('quantity') as FormArray).push(this.formBuilder.control(''));
  }
  removeItem(item: FormGroup, qtyIndex: number, itemIndex: number) {
    (item.get('quantity') as FormArray).removeAt(qtyIndex);
  }
  removeItems(itemIndex: number) {
    const form = this.itemsForm.get('items') as FormArray;
    form.removeAt(itemIndex);
  }
  checkArray(itemIndex: number) {
        // Get the quantity array lenght to display the remove ITEMS button
        return ((this.itemsForm.get('items') as FormArray)
        .at(itemIndex)
        .get('quantity') as FormArray).length;
  }
  onFormSubmit() {
    console.log('Submit : ', this.itemsForm.value);
  }
  ngOnDestroy() {
    this.obsSubscription.unsubscribe();
  }
}

最后,您应该更改:

public dbData: DataModel[];-> public dbData: DataModel;

this.dbData = value-> this.dbData = { ITEMS: value };

dbdata是DataModel[],您可以访问它为this.dbData.ITEMS,这肯定不存在。所以你要么必须

dbData : DataModel;
this.dbData.ITEMS...

dbData : DataModel[];
this.dbData.forEach(dbItem => {
   dbItem.ITEMS...
})

我还注意到DataModel里面有一个数组,我想您打算让一个datamodel而不是数组

这是源代码,带有红色代码中可以看到的错误。

带有错误的源代码

最新更新