【问题标题】:Error: Error trying to diff > 'TypeError'. Only arrays and iterables are allowed错误:尝试 diff > 'TypeError' 时出错。只允许使用数组和可迭代对象
【发布时间】:2017-12-29 21:21:20
【问题描述】:

我想添加表单,但它没有添加到数据库 API Symfony 后端和 Angular 前端,并且列表中也没有显示任何内容。有人知道为什么吗?

AddUserComponent.html:16 错误 在DefaultIterableDiffer.webpackJsonp.../../../core/@angular/core.es5.js.DefaultIterableDiffer.diff (core.es5.js:6843) 在 NgForOf.webpackJsonp.../../../common/@angular/common.es5.js.NgForOf.ngDoCheck (common.es5.js:1691) 在 checkAndUpdateDirectiveInline (core.es5.js:10846) 在 checkAndUpdateNodeInline (core.es5.js:12341) 在 checkAndUpdateNode (core.es5.js:12284) 在 debugCheckAndUpdateNode (core.es5.js:13141) 在 debugCheckDirectivesFn (core.es5.js:13082) 在 Object.eval [as updateDirectives] (AddUserComponent.html:21) 在 Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:13067) 在 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>

表格

 <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) {        
      }        
}

【问题讨论】:

    标签: angular api symfony


    【解决方案1】:

    您正在将this.errors = 设置为一个变量addError,其中包含一个类型可能不是数组的错误。

    尝试改变:

    addError => this.errors = addError
    

    收件人:

    addError => this.errors.push(addError)
    

    我还会重新格式化函数,如下所示:

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

    【讨论】:

    • 我重新格式化函数然后我有这个错误“有一个错误:字段error.json不是函数”确实知道为什么@Wallace
    • 不要接住再扔。只需在使用服务时处理异常。从userService.addUser 中删除.catch(this.handelError)
    • 现在我遇到了这个错误@wallace“有一个错误:字段 Unexpected token
    • 试试.map(res =&gt; res.json() || [])
    • 我现在知道这个错误是什么意思“有一个错误:字段 error.json 不是函数”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 2021-07-23
    • 2021-01-09
    • 1970-01-01
    • 2018-09-14
    • 2019-10-26
    相关资源
    最近更新 更多