2017-12-27 5 views
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

당신이 슈어 있습니까 stylistDetails와 데이터가 동일한 유형입니까? –

+0

예 모두 동일한 유형입니다 –

+3

'Stylist []'에 데이터를 할당 할 때 'Stylist'를 사용하는 유형 중 하나가 일치해야합니다. 두 번째 것은 당신이 기대하고있는 응답 유형을 말하지 않고, Angular가 Object를 리턴 할 때, 당신은'Stylist []'에 할당하려고합니다. Angular에게'Stylist []'타입의 데이터를 기대한다고 말하십시오 : 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; 
    } 
);