Angular2: TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined
Angular2: TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined
我是 observables 的新手,正在尝试使用 Christoph Burgdorf 的 Observables in Angular2 blog 的基本自动完成变体。 运行 此代码生成:
异常:类型错误:无法读取未定义的 属性 'Symbol(Symbol.iterator)'
在 ingredientservice...rawsearch 中发出 REST get 调用后。警报还会弹出 [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);
}
}
已更新代码段并提供修复建议。
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;
}
视图绑定时项目未定义 - 在服务之前 returns。将项目初始化为空数组。或者在模板中使用 Observable 包装器和异步运算符。
我是 observables 的新手,正在尝试使用 Christoph Burgdorf 的 Observables in Angular2 blog 的基本自动完成变体。 运行 此代码生成:
异常:类型错误:无法读取未定义的 属性 'Symbol(Symbol.iterator)'
在 ingredientservice...rawsearch 中发出 REST get 调用后。警报还会弹出 [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);
}
}
已更新代码段并提供修复建议。
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;
}
视图绑定时项目未定义 - 在服务之前 returns。将项目初始化为空数组。或者在模板中使用 Observable 包装器和异步运算符。