Problems with angular requests

0

Good afternoon, I'm starting a new stage and a few days later I'm stuck on a task that I can not understand the failure, I must make a registration form that communicates with an API already done and after many attempts the error persists: "Can not read property 'value' of undefined" which points to the following snippet of code:

    <form #clienteform="ngForm" (ngSubmit)="onSubmit()">

where the inputs are encoded as follows:

     <input type="text" class="form-control" name="Documento" #Documento 
     [(ngModel)]="clienteService.selectedCliente.Documento" 
     placeholder="Documento">

    <input type="text" class="form-control" name="Nome" #Nome 
    [(ngModel)]="clienteService.selectedCliente.Nome" placeholder="Nome">

The POST method in the client.service class works as follows:

    @Injectable()
    export class ClienteService {

    selectedCliente : Cliente;
    clienteList : Cliente[];
    constructor(private http : Http) { }
    urlConfig = 'http://mylookbook.com.br/EasyApi/api/Cliente/';


    postCliente(emp : Cliente){   
    var body = JSON.stringify(emp);
    var headerOptions = new Headers({'Content-Type':'application/json'});
    var requestOptions = new RequestOptions({method : 
    RequestMethod.Post,headers : headerOptions});
    return this.http.post(this.urlConfig+'Create',body,
    requestOptions).map(x => x.json());


    }   

and finally, the onSubmit () method in client.component:

     onSubmit(form: NgForm) {

if (form.value.ClienteId == null) {
  this.clienteService.postCliente(form.value)
    .subscribe(data => {
      this.resetForm(form)
      this.clienteService.getClienteList()
      this.toastr.success('New Record Added Succcessfully', 'Cliente Register')
    })
}

else {
  this.clienteService.putCliente(form.value.ClienteId, form.value)
  .subscribe(data => {
    this.resetForm(form)
    this.clienteService.getClienteList()
    this.toastr.info('Record Updated Successfully!', 'Cliente Register')
  })
 }
}

The providers and imports are already configured correctly, but apparently "selectedClient" stores the values but the POST method does not send them, could someone tell you where the inconsistency is in the code?

Thank you in advance

    
asked by anonymous 04.12.2018 / 16:15

1 answer

0

See that in your method SubSubmit you expect a ngForm and when you call it in the html you are not passing anything.

<form #clienteform="ngForm" (ngSubmit)="onSubmit(clienteform)">
    
04.12.2018 / 17:05