2017-04-05 149 views
1

私はdetails.tsファイルの機能でgoogle Apiからデータを取得しています。 type-api.service.tsファイル内のdisplaingの活字体エラーProperty 'then' does not exist on type 'void'プロパティ 'then'はionic2の型 'void'に存在しません

this.typeApi.getTypeDetails(baseUrl).then(data => { 
}); 

、私は仕事に、このためにpackage.jsonファイルで

import { Injectable } from '@angular/core'; 
import { Http /*, Response*/ } from '@angular/http'; 
import 'rxjs/Rx'; 
import { Observable } from 'rxjs/Observable'; 



@Injectable() 
export class TypeApi { 

constructor(public http: Http) {} 

getTypeDetails(PlaceUrl){ 
     this.http 
     .get(PlaceUrl) 
     .map(res => res.json()) 
     .subscribe(
      (data) => data, 
      (err) => err); // Reach here if fails 
} 

} 

"dependencies": { 
"@angular/common": "2.4.8", 
"@angular/compiler": "2.4.8", 
"@angular/compiler-cli": "2.4.8", 
"@angular/core": "2.4.8", 
"@angular/forms": "2.4.8", 
"@angular/http": "2.4.8", 
"@angular/platform-browser": "2.4.8", 
"@angular/platform-browser-dynamic": "2.4.8", 
"@angular/platform-server": "2.4.8", 
"@ionic-native/core": "3.1.0", 
"@ionic-native/geolocation": "^3.4.4", 
"@ionic-native/launch-navigator": "^3.4.4", 
"@ionic-native/splash-screen": "3.1.0", 
"@ionic-native/status-bar": "3.1.0", 
"@ionic/storage": "2.0.0", 
"font-awesome": "^4.7.0", 
"ionic-angular": "2.3.0", 
"ionic2-rating": "^1.2.0", 
"ionicons": "3.0.0", 
"rxjs": "5.0.1", 
"sw-toolbox": "3.4.0", 
"zone.js": "0.7.2" 
    }, 
"devDependencies": { 
"@ionic/app-scripts": "1.1.4", 
"typescript": "2.0.9" 
}, 

答えて

1

とのより良い方法

getTypeDetails(PlaceUrl){ 
    return this.http 
     .get(PlaceUrl) 
     .map(res => res.json()); 
} 

はまた、私はあなたがメソッド(関数)戻り値の型を設定することをお勧めしたい
、ギュンターwhith同意です呼び出し側が受け取る結果のタイプを確認するために、可能な限りどこでも可能です。上記のケアのための :

あなたTypeApiは、複数の発信者

public getTypeDetails(apiUrl: string) : Observable<any>{ 
     return this.http.get(apiUrl) 
      .map(res => res.json()) 
      .catch((error) => this.handleError(error));; 
} 

//例機能handleErrorのに再利用されると仮定すると、このメソッドを呼び出す

private handleError (error: any) { 
    // Could dig deeper into the error to get a better message or use a remote logging infrastructure 
    let errMsg = (error.message) ? error.message : error.status ? `${error.status} - ${error.statusText}` : 'Server error'; 
    console.error(errMsg); // log to console instead 
    return Observable.throw(errMsg); 
    } 
その後

、メソッドが返す結果にサブスクライブする必要があります。

this.typeApi.getTypeDetails(theUrl).subscribe(result => { 
    //here you have the analog success function from an ajax call 
    //use the received data 
    }, 
    error => { 
     //here you have the analog error function from an ajax call 
     //treat the error 
    } 
); 
2

、怒鳴るなどのGoogle APIからデータを取得していPromisegetTypeDetailsから

に戻す必要があります
getTypeDetails(PlaceUrl){ 
    return this.http 
     .get(PlaceUrl) 
     .map(res => res.json()) 
     .toPromise(); 
} 

ちょうどObservableを返すことは、通常

this.typeApi.getTypeDetails(baseUrl).subscribe(data => { 
}); 
+0

'プロパティ' subscribe 'がタイプ' Promise <{}> 'に存在しません。' Typescriptエラー。 –

+0

'toPromise()'を使うと、質問のコードのように 'then'を使う必要があります。 'toPromise()'を使わなければ、私の答えの2番目の例のように 'subscribe()'を使うことができます。どういうわけか、これら2つのアプローチを混在させているようです。 –

+0

私は 'toPromise()'を使用しませんでしたが、それでも 'Property 'subscribe'がタイプ 'Promise <{}>' Typescriptエラーに存在しませんでした。 –

関連する問題