问题
I get this error message: TS2339: Property 'ITEMS' does not exist on type 'DataModel[]'
I'm working in IONIC/ANGULAR and doing a reactive Form with a model and getting data from HTTP.
There is the model (model.ts):
export class DataModel {
public ITEMS: any[];
constructor(public NAME: string, public QUANTITY: Array<string>) {
this.ITEMS = [{NAME, QUANTITY}];
}
}
There is the code part where the error come from of the 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)
})
);
});
}
There is the full code form.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();
}
}
Thanks for your help
回答1:
dbData is a DataModel[]
, you access it as this.dbData.ITEMS
which for sure doesn't exists. so either you have to
dbData : DataModel;
this.dbData.ITEMS...
or
dbData : DataModel[];
this.dbData.forEach(dbItem => {
dbItem.ITEMS...
})
also I noticed DataModel
has an array inside, I guess you meant to have one DataModel not an array
回答2:
since you defined public dbData: DataModel[]
as an array you should treat it as one.
My guess is you meant to define it as public dbData: DataModel
, then it will work when you call this.dbData.ITEMS.forEach(element => {
Here is the full change:
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();
}
}
Finally you should change:
public dbData: DataModel[];
-> public dbData: DataModel;
this.dbData = value
-> this.dbData = { ITEMS: value };
回答3:
Ther is the source code with the error you can see in the code in red.
Source Code with the error
来源:https://stackoverflow.com/questions/56888106/error-ts2339-property-items-does-not-exist-on-type-datamodel