0

I want to create a new user in my application using angular 4. I add a new user by using a form. I can add him now without API now I want to use API so I want to send this form to API

when click saves form after entering required data function :

employes: Employees[]=[
  // new Employees('aya','test','aya@test.com',45656,true,false)
];

onEmployeeSave(employee: Employees){

  let newEmployee = {
   name: employee.name,
   job: employee.job,
   email: employee.email,
   mob: employee.mob,
   isActive: employee.isActive,
   isMember: employee.isMember
 }
 this.dataStorageService.storeEmployee(newEmployee);        
 this.employes.push(newEmployee);
 this.addEmployeeForm.reset();
 this.modalRef.close();
}

and that's data-storage.service.ts:

getEmployees(): Observable<any>{
    return this.http.get('http://api.###.com/api/Employees/getEmployees');
  }
  storeEmployee(employes: Employees){
  return this.http.post('http://###.com/api/employees/PostEmployee?empId=1&name='+name+'&job='+job+'&email='+email+'&isActive='+isActive+'&mobile='+mob+'&councilMember='+isMember+'&jobId=1', employes );
}

employees.component.html:

        <!-- pop up body -->
            <div class="modal-body" >
            <form [formGroup]="addEmployeeForm" (ngSubmit)="onSubmit(f)" #f="ngForm">
            <div class="row">
             <div class="form-group" class="col-lg-12">
               <ul class="flex-outer">
                <li>
                 <label for="first-name"> name </label>
                  <input type="text" ngModel name="name" formControlName="name">
                </li>
                <li>
                 <label for="job"> job</label>
                  <select ngModel name="job" formControlName="job" required  > 
                    <option *ngFor="let job of jobs" [value]="job.name"> {{ job.name }} </option>
                   </select>
                  </li>
                   <li>
                   <label for="email">email</label>
                   <input type="email" ngModel name="email" formControlName="email" >
                   </li>
                    <li>
                      <label for="mobile"> mob </label>
                       <input type="number" ngModel name="mob" formControlName="mob">
                    </li>
                    <li>
                      <label for="check"> active</label>
                      <div class="form-check">
                       <label class="form-check-label">
                         <input type="checkbox" class="form-check-input" ngModel name="isActive" formControlName="isActive">
                       </label>
                     </div>
                     </li>
                    <li>
                      <label for="check">member  </label>
                        <div class="form-check">
                          <label class="form-check-label">
                          <input type="checkbox" class="form-check-input" ngModel name="isMember" formControlName="isMember">
                           </label>
                         </div>
                         </li>
                        </ul>
                      </div> 

         <button type="submit" class="btn btn-success" (click)="onEmployeeSave(addEmployeeForm.value)"> حفظ </button>

                                </form>
                            </div>
                        </div>


            <div style="overflow-x:auto;">
                <table class="table table-hover table-condensed text-center">
                    <thead>
                        <tr>
                            <th> name </th>
                            <th> job </th>
                            <th> email </th>
                            <th> mob </th>
                            <th> member </th>
                            <th> active </th>
                            <th> edit</th>
                        </tr>
                    </thead>
                    <tbody>
                         <tr *ngFor="let employee of employes">
                            <td> {{employee.Emp_Name}} </td>
                            <td> {{employee.Job}} </td>
                            <td> {{employee.Email}} </td>
                            <td> {{employee.Mobile}} </td>
                            <td> {{employee.Is_Active}} </td>
                            <td> {{employee.Council_Member}} </td>
                            <td> 

                            </td>
                        </tr> 
                    </tbody>
                </table>

I have an error: can't find name 'mob', can't find name 'job', can't find name 'email', can't find name 'isMember', can't find name 'isActive'

any help?

Aya Abdelaziz
  • 355
  • 3
  • 10
  • 23

1 Answers1

1

Modify your storeEmployee method:

storeEmployee(employes: Employees){
  return this.http.post('http://###.com/api/employees/PostEmployee?empId=1&name='+employes.name+'&job='+employes.job+'&email='+employes.email+'&isActive='+employes.isActive+'&mobile='+employes.mob+'&councilMember='+employes.isMember+'&jobId=1', employes );
}

Try this to check the response from server:

storeEmployee(employes: Employees){
    let url = 'http://###.com/api/employees/PostEmployee?empId=1&name='+employes.name+'&job='+employes.job+'&email='+employes.email+'&isActive='+employes.isActive+'&mobile='+employes.mob+'&councilMember='+employes.isMember+'&jobId=1';
    let headers = new Headers({ 'Content-Type': 'text/plain' });
    let options = new RequestOptions({ headers: headers }); 
    return this.http.post(url, JSON.stringify(employes), options)
        .map((res: Response) => res.json())
        .subscribe(
            data => {
                console.log(data);
                return true;
            },
            error => {
                console.log(error);
                console.error("Error saving employes!");
                return false;
            }
    );
}
Surender Khairwa
  • 601
  • 4
  • 17
  • now i don't have error but also it doesn't store data :/ – Aya Abdelaziz Sep 26 '17 at 14:07
  • Add console.log in your storeEmployee method and check if you are getting values here `storeEmployee(employes: Employees){ console.log('employes.name=' + employes.name);` – Surender Khairwa Sep 26 '17 at 14:16
  • it logs the data i entered but doesn't save it in database , it disappear after refreshing – Aya Abdelaziz Sep 26 '17 at 14:23
  • What is the http error code ? You can check in the browser F12> Network Tab to see the https call. You can also try http clients to first make sure your backend is working. [POSTER](https://chrome.google.com/webstore/detail/chrome-poster/cdjfedloinmbppobahmonnjigpmlajcd?hl=en) – Surender Khairwa Sep 26 '17 at 15:21
  • XMLHttpRequest cannot load http://api.###.com/api/Employees/postEmployee?empId=1&name=tgrth&job=2&email=fghfgh&isActive=true&mobile=435345&councilMember=true&jobId=1. Response for preflight has invalid HTTP status code 405 but by using POSTER it post data correctly – Aya Abdelaziz Sep 26 '17 at 15:36
  • Ok I got the issue you need to set header in your request: see this: https://stackoverflow.com/a/46123053/2015408. See my updated answer. – Surender Khairwa Sep 26 '17 at 16:38
  • the same error of XMLHttpRequest and it logs undefined then error saving employs – Aya Abdelaziz Sep 27 '17 at 08:28
  • Do u know any thing about this error '{"$id":"1","Message":"The requested resource does not support http method 'OPTIONS'."}' ??? – Aya Abdelaziz Sep 27 '17 at 08:32
  • Yes this is what I said the problem is you need to set the headers Content-Type to something like 'text/plain' so that it does not issue a preflight http OPTIONS call which your server is not supporting. You need to refer to the link I provided above. Also if you could add your code to stackblitz or plunker it would be easier to debug. – Surender Khairwa Sep 27 '17 at 08:39
  • i add your updated answer to my code but i still have the same problem , i don't know alot of things about http request and the link you refer didn't solve my problem :/ – Aya Abdelaziz Sep 27 '17 at 08:47
  • Please add your code here so that I can take a look: https://stackblitz.com/edit/angular-euqgyv – Surender Khairwa Sep 27 '17 at 08:55
  • i change headers to let headers = new Headers({ 'Content-Type': 'text/plain' }); now no error but no data posted immediately , data posted after refreshing .. do u have any idea why ? – Aya Abdelaziz Sep 27 '17 at 08:56
  • Use JSON.stringify(employes) instead of just employes. – Surender Khairwa Sep 27 '17 at 09:00
  • yes , i used it but it didn't post the data without refreshing? do u need now to add my code to stackblitz? – Aya Abdelaziz Sep 27 '17 at 09:04
  • You need to set the data you received from server back on to your page. You are getting the data on this line `console.log(data);` Also move the .subscribe to your component so that you can update the data you get from the server in your component. See this [tutorial](https://scotch.io/tutorials/angular-2-http-requests-with-observables) – Surender Khairwa Sep 27 '17 at 09:48
  • this.dataStorageService.storeEmployee(newEmployee) .subscribe( data => { this.employes.push(newEmployee); console.log(data); return true; }, error => { console.log(error.json().error); console.error("Error saving employes!"); return false; } ); i change onEmployeSave to this but i still have the problem :/ – Aya Abdelaziz Sep 27 '17 at 10:04
  • i solve it by add getEmployee function in response of storeEmployee function but i think it's stupid solution and overloading of getting all data everytime i add user – Aya Abdelaziz Sep 27 '17 at 10:09