0

I have component called customer which is used to add new customers. The component code as follows:

HTML

<div >
  <form [formGroup]="addForm">

        <div>
          <mat-form-field>
            <input matInput placeholder="First Name" formControlName="firstname" required>
            <mat-error *ngIf="addForm.controls.firstname.hasError('required')">
              Please enter first name
            </mat-error>
          </mat-form-field>
        </div>

        <div>
          <mat-form-field>
            <input matInput placeholder="Last Name" formControlName="lastname" required>
            <mat-error *ngIf="addForm.controls.lastname.hasError('required')">
              Please enter last name
            </mat-error>
          </mat-form-field>
        </div>


        <div>
          <mat-radio-group formControlName="gender">
            <div>Gender</div>
            <mat-radio-button  value="Male">Male</mat-radio-button>
            <mat-radio-button value="Female">Female</mat-radio-button>
          </mat-radio-group>
        </div>

        <div>
          <mat-form-field>
            <input matInput placeholder="Email Address"  formControlName="email" required>
            <mat-error *ngIf="addForm.controls.email.hasError('required') ">
              Please enter email address
            </mat-error>
          </mat-form-field>
        </div>


    <div>
        <button mat-raised-button (click)="onAdd()">ADD</button>
    </div>

  </form>

TS

  import { Component, OnInit, VERSION, ViewChild } from '@angular/core';
  import {
    FormBuilder,
    FormControl,
    FormGroup,
    Validators,
  } from '@angular/forms';
 import { Router } from '@angular/router';
 import { IContact } from 'src/app/models/app.models';
 import { CustomersService } from 'src/app/services/customers.service';

 @Component({
   selector: 'asd-customer',
   templateUrl: './customer.component.html',
   styleUrls: ['./customer.component.css'],
 })

export class CustomerComponent implements OnInit {
 public addForm: FormGroup;
 public someContact: IContact;

 constructor(private fb: FormBuilder,
      public customersService: CustomersService) {}

public ngOnInit(): void {
 this.addForm = this.fb.group({
   firstname: [null, [Validators.required],
   lastname: [null, [Validators.required],
   email: ['', [Validators.required],
   gender: [null],

  });
 }

 public onAdd(): void {
   this.someContact = this.addForm.value;
   this.customersService.addCustomer(this.someContact);
 }


}

model.ts file

export interface IContact {
  firstName:      string;
  lastName:       string;
  gender:         string;
  eMailAddresses: string[];
}

The expected JSON after the POST request:

 {
   "firstName": "Alfien",
   "lastName": "Urlich",
   "gender": "Male",
   "eMailAddresses": ["[email protected]"],
  }

When i fill every input fields of the form and tried performing http POST operation. I am getting warning as Bad Request because of email. Below is the warning:

enter image description here

When i perform POST operation without filling email(input field) the POST happens fine, the JSON appears like this:

  {

   "firstName": "Lee",
   "lastName": "Cooper",
   "gender": "Male",
}

What's wrong with the code ??

9
  • you are using json.parse somewhere in code? Commented Dec 18, 2018 at 9:37
  • No i am not using. Commented Dec 18, 2018 at 9:37
  • sorry , my bad JSON.stirngify? Commented Dec 18, 2018 at 9:38
  • I am not using both methods. Commented Dec 18, 2018 at 9:40
  • @PrashanthGH check your back what they want or which type of eMailAddresses this field string or string array? Commented Dec 18, 2018 at 9:44

1 Answer 1

4

There is a problem in Email-address field In Form-control it is an single string and in interface it is an array.So if u want to add it set it like below,

public someContact: IContact={};
    public onAdd(): void {
     this.someContact.firstName = this.addForm.value.firstName;
     this.someContact.lastname = this.addForm.value.lastname;
     this.someContact.gender = this.addForm.value.gender; 
     this.someContact.eMailAddresses=[]
     this.someContact.eMailAddresses.push(this.addForm.value.email);
    }

change interface as follows

export interface IContact {
  firstName ?:      string;
  lastName ?:       string;
  gender ?:         string;
  eMailAddresses ?: string[];
}

but according to your scenario there will be only one email address in everytime you submit.

Sign up to request clarification or add additional context in comments.

9 Comments

Getting this warning : ERROR TypeError: this.someContact.eMailAddresses.push is not a function
please initialize someContact as public someContact: IContact={} and before pushing intialize array as this.someContact.eMailAddresses=[ ]
Now the email as stored in DB as null
I tried setting public someContact: IContact = {}; Getting this warning Type '{}' is not assignable to type 'IContact'.
is the expected JSON is correct? can u show me the JSON after submitting
|

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.