Angular 2搜索服务错误:res.json不是函数

时间:2017-08-22 20:00:47

标签: javascript json angular

我正在构建一个角度2服务来搜索特定信息。我的服务几乎正常,但我一直收到这个错误:

core.es5.js:1020 ERROR TypeError: res.json is not a function at MapSubscriber.project

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/switchMap';


import { FundraiserProgressService } from 'app/services/fundraiser-progress.service';

@Injectable()
export class SearchService {
  baseUrl: String = 'http://localhost:4402/items';
  queryUrl: string = '?search=';

  constructor(private http: HttpClient) { }

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

  searchEntries(term) {
    return this.http
      .get(this.baseUrl + this.queryUrl + term)
      .map((res: Response) => res.json())
  }
}

1 个答案:

答案 0 :(得分:1)

这是因为使用Angular版本4.3.0中的新HttpClient,JSON是假定的默认值,不再需要显式解析。因此,您可以删除valgrind --leak-check=yes --show-reachable=yes ./myProgram

以下是documentation

的示例
.map((res: Response) => res.json())