2016-11-22 12 views
1

私はSO Postの顔と同じ挑戦をしていますhere私のサービスではデータがあるのに、私のcomponent.tsのsubscribeメソッドで未定義になっています。あなたはそれがfunctionからの明示的なリターンを必要と{}を使用されるように p.component.ts角2のobservable-subscribeが未定義であることを示しています

private getPayItems():void{ 
    console.log('In getPayItems'); 
    this._payItemService.getPayItems() 
    .subscribe(data => { 
     this.payItemArray = data; 
     console.log(data); 
    }, 
    (error:any) =>{ 
     this.alerts.push({ msg: error, type: 'danger', closable: true }); 
    }) 
} 

p.service.ts

getPayItems():Observable<Payitem[]>{ 

    let actionUrl = this.url + "/GetPayItem"; 

    return this._http.get(actionUrl, { headers: this.headers }) 
     .map((response: Response) => { 
      <Payitem[]>response.json() ; 
      console.log(<Payitem[]>response.json()); //This logs the Object 
     }) 
     .catch(this.handleError); 
} 

答えて

5

以下のコードを参照してください。だからmapの機能から<Payitem[]>response.json()を返さなければならない。それ以外の場合は、以下の

getPayItems():Observable<Payitem[]>{ 

    let actionUrl = this.url + "/GetPayItem"; 

    return this._http.get(actionUrl, { headers: this.headers }) 
     .map((response: Response) => { 
      console.log(<Payitem[]>response.json()); //This logs the Object 
      return <Payitem[]>response.json() ; 
     }) 
     .catch(this.handleError); 
} 

はあなたがちょうど、私の一日先生を救っ速記構文

getPayItems():Observable<Payitem[]>{ 
    let actionUrl = `${this.url}/GetPayItem`; 
    return this._http.get(actionUrl, { headers: this.headers }) 
     .map((response: Response) => <Payitem[]>response.json()) 
     .catch(this.handleError); 
} 
+0

だろう!ありがとうございました –

関連する問題