私はangularJs2を初めて使用しています。タイプPromise <void>はPromise <customType []>タイプに割り当てられません
import { Injectable, OnInit } from '@angular/core';
import { customType } from '../models/currentJobs';
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class JobService implements OnInit {
constructor(private http: Http) { }
ngOnInit(): void {
this.getCurrentJobs();
}
private headers: Headers = new Headers({ 'Content-Type': 'application/json' });
private ordersUrl: string = 'http://localhost:35032/api/order/';
public orders: customType[];
getCurrentJobs(): Promise<customType[]> {
var jobs = this.http.get(this.ordersUrl)
.toPromise()
.then(response => {
this.orders = response.json() as customType[];
})
.catch(this.handleError);
return jobs;//this line throws error
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
}
私はエラー
**TS2322 Build:Type 'Promise<void>' is not assignable to type 'Promise<customType[]>'.*
*
の構成をコンパイルし、次のとおりです。私は、次のサービスを作成しました
このエラーを修正するのに手伝ってください。
は '.then()' 'return'を持っているべきですか?それがなければ、「仕事」がどのような価値を持っていると思いますか? – nnnnnn
.then()は、これまでコールバックが完了したことを示すPromiseを返します。私はthen()のドキュメントでこれを見つけました –
あなたが探している答えを見つけたことに感謝しますが、私の更新された答えはまだ投票に値しますか?それが間違っていれば、それは問題ありませんが、私はそれがだとは思わないし、下の投票は、後で問題を解決する可能性のある解決策として検討することを妨げるでしょう。ありがとう! –