1
votes

I want to add form but it didn't added in database API Symfony backend and Angular Frontend and in the list display nothing too. Anyone know why?

AddUserComponent.html:16 ERROR atDefaultIterableDiffer.webpackJsonp.../../../core/@angular/core.es5.js.DefaultIterableDiffer.diff (core.es5.js:6843) at NgForOf.webpackJsonp.../../../common/@angular/common.es5.js.NgForOf.ngDoCheck (common.es5.js:1691) at checkAndUpdateDirectiveInline (core.es5.js:10846) at checkAndUpdateNodeInline (core.es5.js:12341) at checkAndUpdateNode (core.es5.js:12284) at debugCheckAndUpdateNode (core.es5.js:13141) at debugCheckDirectivesFn (core.es5.js:13082) at Object.eval [as updateDirectives] (AddUserComponent.html:21) at Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:13067) at checkAndUpdateView (core.es5.js:12251) View_AddUserComponent_0 @ AddUserComponent.html:16 proxyClass @ compiler.es5.js:14985 webpackJsonp.../../../core/@angular/core.es5.js.DebugContext_.logError @ core.es5.js:13407 webpackJsonp.../../../core/@angular/core.es5.js.ErrorHandler.handleError

add-user.components.ts

import { Component, OnInit } from '@angular/core';
import {UserService} from '../user.service';
import {Router} from '@angular/router';

@Component({
  selector: 'app-add-user',
  templateUrl: './add-user.component.html',
  styleUrls: ['./add-user.component.css']
})
export class AddUserComponent implements OnInit {

  username: string ;
  email: string;

  errors= [];


  constructor(private _userService: UserService , private router: Router) { }

  addUser(username, email) {

    let user: any;
    user = {username: username, email: email};
    this._userService.addUser(user).subscribe(( result => {

      this.router.navigate(['/users']);

    }), addError => this.errors = addError);

  }
  ngOnInit() {
  }
}

<form (submit)="addUser(username,email)" class="well">

<div class="form-group">
<label>username:</label>
<input type="text" class="form-control" placeholder="add username"
     [(ngModel)]="username" name="username" >
</div>

form

 <div class="form-group">
    <label>Email:</label>
    <input type="text" class="form-control" placeholder="add email"  [(ngModel)]="email" name="email" >
  </div>



      <div *ngFor="let error of errors" class="alert alert-danger">
          <div>There is an error in :{{error.field}} field</div>
          <div>{{error.message}}</div>
        </div>

  <button type="submit" class="btn btn-primary" >Save</button>

</form>

user.service

 @Injectable()
 export class UserService {

  private uri= 'http://127.0.0.1:8000/api/users';
  constructor(private http: Http, private authenticationService: AuthService  ) {}

  getUsers(): Observable<any[]> {
    const headers = new Headers({ 'Authorization': 'Bearer ' + this.authenticationService.token });
    return  this.http.get(this.uri , {headers : headers}).map(res => <User[]> res.json() )
    .catch(this.handelError);

  }
  addUser(user: User) {
    const  headers = new Headers();
    headers.append('content-type', 'application/json');
    headers.append('Authorization', 'Bearer ' + this.authenticationService.token);
    return this.http.post(this.uri, JSON.stringify(user), {headers : headers})
    .map(res => res.json()).catch(this.handelError);
  }

user.ts

export class User {        
    constructor(public id, public username: string, public email: string) {        
      }        
}
1

1 Answers

0
votes

Your are setting and array this.errors = to a variable addError that contains an ERROR who's type is probably not an Array.

Try changing:

addError => this.errors = addError

To:

addError => this.errors.push(addError)

I would also reformat the function to look like this:

this._userService.addUser(user).subscribe(
  result => this.router.navigate(['/users']),
  err => this.errors.push(err)
);