2017-05-16 17 views
1

Observablesサービスレイヤの呼び出しに問題があります。サービスレイヤまでデータを取得していますが、何が間違っているかわからないコンポーネントレイヤに伝播しません。私はここで何か間違っていますか? DNSComponent.ts角度:観測可能、コンポーネントクラスにデータを返さないで購読する

result: Dns[]; 
this.dnsSvc 
       .getDNS() 
       .subscribe(
        res => { 
         console.log('In component result class:===>' + res); 
         this.result = res; 
         console.log('In component class:===>' + this.result); 
        }, 
       // make sure you get data here. 
       // console.log('Data from API layer', +res); 
        error => console.log('Error from backend API', +error), 
      () => console.log('Done') 
      ); 

HelperUtils.tsで

(これは、HTTP REST APIの同じクラスである)

getResource(url: string): Observable<any[]> { 
     this.headers.set('token', token); 
     this.options = new RequestOptions({ headers: this.headers }); 
     console.log('URL at HttpUtils', this.getServer() + url); 
     console.log('Options at HttpUtils', this.options); 
     return this.http 
      .get(this.getServer() + url, this.options) 
      .map(this.retrieveData) 
      .catch(this.handleException); 
} 
DNSService.tsで

getDNS(): Observable<Dns[]> { 
     return this.helperUtils 
      .getResource('databases') 
      .subscribe(
       res => this.data = res, 
        // make sure you get data here. 
        // console.log('Data from Service API layer', this.data), 
       error => console.log('Error from backend API', +error), 
       () => console.log('Done') 
     ); 
    } 

DNSModel.ts

export class Dns { 
    public dnsname: string; 
} 

以下更新されたコード:

model.ts

export class Dns { 
    public dnsname: string; 
} 

helperUtils.ts

// - Simply return the observable of the call 
    getResource(url: string): Observable<Response> { 
     this.headers.set('Api_key', api_key); 
     this.options = new RequestOptions({ headers: this.headers }); 
     console.log('URL at HelperUtils', this.getServer() + url); 
     console.log('Options at HelperUtils', this.options); 
     return this.http.get(this.getServer() + url, this.options); 
    } 

サービス層

data: Dns[]; 
// Subscribe to the result and do the Mapping. 
    getDNS(): Observable<Dns[]> { 
     return this.httpUtils.getResource('databases') 
      .map(res => { 
       if (res.ok) { 
        let body = this.retrieveData(res); 
        return body.map(x => new Dns()); 
       } 
      // console.log('Issue with the response'); 
      } 
     ); 
    } 
    private retrieveData(res: Response) { 
     console.log('Response at httpService -', res.json()); 
     let body = res.json(); 
     console.log('Response at httpService res body-', body); 
     return body; 
    } 
あなたのgetDNS()方法で

Component.ts

result: Dns[]; 

ngOnInit() { 

     this.instanceDetailsSvc.getDNS() 
      .map(res => { 
       this.result = res; 
      }, 
      error => console.log('Error from backend API', +error) 
     ); 
      console.log('The Result Is:::::' + this.result); 
} 

答えて

0

、あなたはthis.result `へgetResource呼び出しの結果を代入されますが、観測可能:

として結果そのものを返すされていません
getDNS(): Observable<Dns[]> { 
    return this.helperUtils 
     .getResource('databases') 
     .subscribe(
      res => this.data = res, // <= HERE 
      error => console.log('Error from backend API', +error), 
      () => console.log('Done') 
    ); 
} 

コンポーネントのコードから、DNSのオブジェクトの配列を受け取るAPI呼び出しを行っていることがわかります。この場合、あなたは呼び出しの結果をDNS[]にマップし、それをコンポーネントからサブスクライブします。

//- Simply return the observable of the call 
getResource(url: string): Observable<any[]> { 
    this.headers.set('token', token); 
    this.options = new RequestOptions({ headers: this.headers }); 
    return this.http.get(this.getServer() + url, this.options); 
} 

...

最後に
// Subscribe to the result and do the Mapping. 
getDNS(): Observable<Dns[]> { 
    return this.helperUtils.getResource('databases') 
      .map(res => { 
        if (res.ok) { 
         // do mapping logic here, i.e. 
         return res.json.map(x => new DNS(x)); 
        } 
        else { 
         //handle invalid result 
        } 
      } 
    ); 
} 

、コンポーネント上:このような何か

this.dnsSvc.getDNS() 
    .subscribe(res => { 
      this.result = res; 
    }, 
    error => console.log('Error from backend API', +error)); 
+0

私は上記の解決策を試してみましたが、私はエラーTS2345の問題を取得しています:引数タイプ 'any []'のタイプは、 'レスポンス'タイプのパラメータに割り当てられません。 getDNS():観測可能 { リターンthis.helperUtils.getResource( 'データベース') の.map(RES => {// AngularTSCompiler 場合(res.ok){//ここ マッピング・ロジックを実行する、すなわち リターンres.json.map(x =>新しいDNS(x)); } else { //無効な結果を処理する } } ); } – Swaroop

+0

正しくマッピングしていますか?この例では、新しいDNS(x)を作成しましたが、独自のマッピングを実装する必要があります。 –

+0

Ormeno、ありがとうありがとうございました。あなたが言及したとおりのやり方で、データをサービス層に渡しています(文字の制限のためコメントにコードを入力できません)。しかし今は別の問題が発生しています。私はサービス層にデータを取得していますが、その時点ではデータは定義されていません。あなたの考えを示唆してください。 – Swaroop

関連する問題