【问题标题】:Angular2: TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefinedAngular2:TypeError:无法读取未定义的属性'Symbol(Symbol.iterator)'
【发布时间】:2016-03-29 21:35:41
【问题描述】:

我是 observables 的新手,我正在尝试 Christoph Burgdorf 的 Observables in Angular2 blog 的基本自动完成变体。运行此代码会产生:

异常:TypeError:无法读取未定义的属性“Symbol(Symbol.iterator)”

在成分服务...rawsearch 中发出 REST 调用后。警报还会弹出 [object Object] 消息。我已验证端点运行正常。

任何关于如何调试它的建议将不胜感激。

ingredientservice.ts 等待文本字符串的更改,对其进行去抖动并执行 REST 调用以从端点获取自动完成匹配。

import {Injectable} from 'angular2/core';
import {Http} from 'angular2/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/map';

@Injectable()
export class IngredientService {
  endpoint_url: String = "http://localhost:5000/ingredient/";
  results: Observable<Array<string>>;
  constructor(private http: Http) { }

  search(terms: Observable<string>, debounceDuration = 400) {
    return terms.debounceTime(debounceDuration)
      .distinctUntilChanged()
      .switchMap(term => this.rawSearch(term));
  }

  getData: Object[];
  rawSearch(term: string) {
    console.log(term);

    this.http.get(this.endpoint_url + term)
      .subscribe(
      res => {
        this.getData = res.json();
        console.log(this.getData);
        return this.getData;
      },
      error => alert(error));
  }
}

为了完整起见,我已包含组件 ingredientsearch.ts

import {Component} from 'angular2/core';
import {Control} from 'angular2/common';
import {IngredientService} from './ingredientservice';
import {Observable} from 'rxjs/Observable';

@Component({
  selector: 'ingredient-search',
  template: `
    <div>
      <h2>Ingredient Search</h2>
      <input type="text" [ngFormControl]="term"/>
      <ul>
        <li *ngFor="#item of items | async">{{item}}</li>
      </ul>
    </div>
  `,
  providers: [IngredientService]
})

export class IngredientSearch {
  items: Observable<Array<string>>;
  term = new Control();
  constructor(private ingredientService: IngredientService) {
    console.log("About to call service");
    this.items = ingredientService.search(this.term.valueChanges);
  }
}

使用推荐的修复更新了代码 sn-p。

rawSearch(term: string) {
    this.getData = ([]);
    this.http.get(this.endpoint_url + term)
      .subscribe(
      res => {
        this.getData = res.json();
        console.log(this.getData);
        return this.getData;
      },
      error => {
        alert(error);
      });
    return this.getData;
  }

【问题讨论】:

    标签: angular angular2-services


    【解决方案1】:

    当视图被绑定时 - 在服务返回之前,Items 是未定义的。将项目初始化为空数组。或者在模板中使用 Observable 包装器和异步运算符。

    【讨论】:

    • 会试一试,但是我应该注意智能服务,我在上面提供的 Angular2 博客链接中的 Observables 中的哑组件代码完美运行。
    • 好吧,我无法对此发表评论,因为我没有详细查看示例,但我知道在使用该原则之前我已经纠正了该错误。祝你好运。
    • 非常感谢 Mojo80,成功了!为了完整起见,我将发布更新的代码。
    • 绝对没问题:)
    • 这帮助我解决了我在使用 Express 时遇到的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 2017-11-03
    • 2019-04-29
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多