Cannot read Property 'name' of Undefined

Asked

Viewed 3,388 times

1

Gentlemen, I’m making this mistake by running my registration form..

ERROR Typeerror: Cannot read Property 'name' of Undefined at Forms.js:3282 at Forms.js:3222 at Array.foreach () At Formgroup.push.. /node_modules/@angular/Forms/fesm5/Forms.js.Formgroup. _forEachChild (Forms.js:3222) At Formgroup.push.. /node_modules/@angular/Forms/fesm5/Forms.js.Formgroup. _checkAllValuesPresent (Forms.js:3281) At Formgroup.push.. /node_modules/@angular/Forms/fesm5/Forms.js.Formgroup.setValue (Forms.js:3071) at ContactformComponent.push.. /src/app/contactform/contactform.component.ts.ContactformComponent.ngOnInit (contactform.component.ts:57) checkAndUpdateDirectiveInline (core.js:9250) checkat AndUpdateNodeInline (core.js:10514) checkat AndUpdateNode (core.js:10476)

Follows my code:

  contactFrm: FormGroup;

  constructor(@Inject(MAT_DIALOG_DATA) public data: any,
                     private fb: FormBuilder,
                     private _contactService: ContactService,
                     public dialogRef: MatDialogRef<ContactlistComponent>) {}  

ngOnInit() {
    // built contact form
    this.contactFrm = this.fb.group({
      //id: [''],
      name:  [''],
      email: ['', [Validators.required, Validators.email]],
      gender: ['', [Validators.required]],
      birth: ['', [Validators.required]],
      techno: ['', [Validators.required]],
      message: ['', [Validators.required]]
    });
}

html:

<form  (ngSubmit)="onSubmit(contactFrm)"  [formGroup]="contactFrm">
    <h2>{{data.modalTitle}}</h2>

    <div>
        <mat-form-field appearance="outline">
        <mat-label>Name</mat-label>
        <input type="text" matInput placeholder="Name" name="name" formControlName="name">
        <!-- <mat-icon matSuffix>sentiment_very_satisfied</mat-icon> -->
        <!-- <mat-hint>Hint</mat-hint> -->
        <mat-error *ngIf="formErrors.name">
          {{ formErrors.name }}
        </mat-error>
      </mat-form-field>
    </div>
</form>

I’m trying to make an example of this website.

  • opa, I think I’m missing putting "ngModel" in your name input. Can you see if this is it?

  • not if that’s it, because in the example it doesn’t use.. I’m running Angular 6

4 answers

1

The error is in the following html:

  <mat-error *ngIf="formErrors.name">
              {{ formErrors.name }}
   </mat-error>

you have not given a property to.

0

Just to confirm, I removed the code snippet below and yet the problem still persists...

{{ formErrors.name }}

Does it have something to do with the angular packets?

0

Good afternoon, try this code snippet.

<mat-error *ngIf="contactFrm.get('name').errors">
                  {{ contactFrm.get('name').errors.required }}
</mat-error>

0

I forgot to put this code in the error text..

I had already put but the problem continues..

formErrors = { 'name': '' 'email': ', 'gender': ' 'Birth': ', 'techno': ' 'message': '' };

  • edit the question since that didn’t solve your problem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.