【问题标题】:Angular 6 Error using RxJS: Cannot read property 'call' of undefined使用 RxJS 的 Angular 6 错误:无法读取未定义的属性“调用”
【发布时间】:2019-02-01 02:41:54
【问题描述】:

我正在尝试将 Angular 5 应用程序迁移到 Angular 6,而我有这一段 RxJS 代码让我很困惑。我已经通过 Google 搜索并阅读了 RxJS 文档,足以知道我应该使用 .pipe,但我无法完全理解语法。

当用户在搜索输入字段中键入内容时,代码会从 API 调用返回结果。新版本目前抛出错误:

ERROR TypeError: Cannot read property 'call' of undefined
    at merge.js:8
    at MapSubscriber.project (switchMap.js:9)

原码:

this.searchResults =
 this.form.controls['search'].valueChanges
 .debounceTime(200)
 .switchMap(query => this.search.searchPeople(query))
 .merge(this.clearSearch.mapTo([]));

新版本:

this.searchResults = this.form.controls['search'].valueChanges.pipe(
debounceTime(200),
switchMap(query => this.search.searchPeople(String(query)),
merge(this.clearSearch.pipe(mapTo([])))));

我已经像这样导入了 RxJS:

import { Observable } from 'rxjs';
import { Subject } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
import { mapTo } from 'rxjs/operators';
import { merge } from 'rxjs/operators';
import { switchMap } from 'rxjs/operators';

【问题讨论】:

  • 我觉得很好,你确定这个错误来自你的这部分代码吗?
  • @martin 你是对的,代码没有任何问题。 API 调用的格式略有变化。

标签: angular typescript rxjs


【解决方案1】:

@martin 是正确的,这段代码没有任何问题。

问题是从 API 调用返回的格式略有变化。它已经被格式化为一个数组,所以我只需要删除最后一行,因为不再需要 mapTo:

// merge(this.clearSearch.pipe(mapTo([])))));

【讨论】:

    猜你喜欢
    • 2018-10-26
    • 2018-06-26
    • 1970-01-01
    • 2022-01-13
    • 2021-05-21
    • 2019-04-15
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多