2017-08-22 6 views
0

特定の情報を検索するために、角度2のサービスを構築しています。私は、サービスはほとんど働いていたが、私はこのエラーを取得しておいてください。角度2の検索サービスのエラー:res.jsonは関数ではありません

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

アンギュラバージョン4.3.0のような新しいHttpClientで、JSONを想定し、デフォルトとはもはや必要があるためです明示的に解析されます。したがって、.map((res: Response) => res.json())を削除できます。ここで

documentationから取った例です:

@Component(...) 
export class MyComponent implements OnInit { 

    results: string[]; 

    // Inject HttpClient into your component or service. 
    constructor(private http: HttpClient) {} 

    ngOnInit(): void { 
    // Make the HTTP request: 
    this.http.get('/api/items').subscribe(data => { 
     // Read the result field from the JSON response. 
     this.results = data['results']; 
    }); 
    } 
} 
関連する問題