1

ERROR TypeError: Cannot read property 'push' of undefined

addAddress() {
    const control = <FormArray>this.AddUserstep2Form.controls['addresses'];  

    const addrCtrl = this.initAddress();

    control.push(addrCtrl);

  }
initAddress() {
    return this.fb.group({
      stateCode: [''],
      cityId: ['']
    }); 
  }

1 Answer 1

2

This is happening when control is null , you need to initialize with empty array

const control = <FormArray>this.AddUserstep2Form.controls['addresses'];  
const addrCtrl = this.initAddress();
if(control && control.length > 0){
 control.push(addrCtrl);
}else
{
 control = [];
 control.push(addrCtrl);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank You for your help. but still I am facing same error
@Sajeetharan ,I created const control in my component as FormArray, can I init with empty (to be added by user)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.