2017-12-27 19 views
1

これは私のアプリケーションの1つのコンポーネントであり、それは完全に動作しますが、この行にはthis.stylistDetails = data;というエラーがあります。誰かがそれを角度httpクライアントエラー

import { Component, OnInit } from '@angular/core'; 
    import {ActivatedRoute} from '@angular/router'; 
    import {HttpClient} from '@angular/common/http'; 

    @Component({ 
     selector: 'app-search-results', 
     templateUrl: './search-results.component.html', 
     styleUrls: ['./search-results.component.css'] 
    }) 
    export class SearchResultsComponent implements OnInit { 

     stylistDetails: Stylist[] = []; 
     need; 
     type; 
     showMessage; 
     preferred_locations = []; 

     countResults; 

     // onNotifyClicked(message:string): void{ 
     // this.showMessage = message; 
     // } 

     onClickLocFiltered(): void{ 
     this.http.get('/api/stylist/getStylist/loc').subscribe(
      data => { 
      this.stylistDetails = data; 
      this.countResults = this.stylistDetails.length; 
      } 
     ); 
     } 

     constructor(private route: ActivatedRoute, private http: HttpClient) { 

     this.http.get<Stylist>('/api/stylist/getStylistName').subscribe(
      data => { 
      this.stylistDetails = data; 
      // console.log(this.stylistName); 
      // console.log(this.stylistName.length); 
      this.countResults = this.stylistDetails.length; 
      } 
     ); 


     } 

     ngOnInit() { 
     this.route 
      .queryParams 
      .subscribe(params => { 
      // Defaults to 0 if no query param provided. 
      this.need = params['need'] || 0; 
      this.type = params['type'] || 0; 
      }); 
     } 
    } 

    interface Stylist { 
     name: string; 
     address: string; 
     cost: number; 
     skills: string[]; 
    } 

を解決する方法を提案そして、これは私の端末が

ERROR in src/app/search-results/search-results.component.ts(27,9): error TS2322: Type 'Object' is not assignabl 
e to type 'Stylist[]'. 
    The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? 
src/app/search-results/search-results.component.ts(27,9): error TS2322: Type 'Object' is not assignable to type 
'Stylist[]'. 
    The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? 
    Property 'includes' is missing in type 'Object'. 
src/app/search-results/search-results.component.ts(37,9): error TS2322: Type 'Stylist' is not assignable to typ 
e 'Stylist[]'. 
src/app/search-results/search-results.component.ts(37,9): error TS2322: Type 'Stylist' is not assignable to typ 
e 'Stylist[]'. 
    Property 'includes' is missing in type 'Stylist'. 
+0

あなたはSHUREですstylistDetailsとデータには同じタイプがありますか? –

+0

はい両方とも同じタイプです –

+3

あなたのタイプは一致する必要があります。一つはあなたが 'Stylist []'にデータを割り当てるときに 'Stylist'を使用しています。 2番目は、どのタイプの応答を期待しているのかを言っておらず、Angularがオブジェクトを返すので、あなたは 'Stylist []'に割り当てようとしています。 「Stylist []」のデータを期待していることをAngularに教えてください:this.http.get ( '/ api/stylist/getStylist/loc') 'https://angular.io/guide/http#typechecking-the-応答 – Alex

答えて

1

の変更は、あなたがこれにコンストラクタからメソッドを購読示したものを誤りであることができます。

this.http.get<any>('/api/stylist/getStylistName').subscribe(
    data => { 
    this.stylistDetails = data; 
    // console.log(this.stylistName); 
    // console.log(this.stylistName.length); 
    this.countResults = this.stylistDetails.length; 
    } 
);