2017-05-09 29 views
0

私は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[]>'.* *

次取得のVisual Studio 2017を使用してコードをコンパイルすると、私の活字体はVs2017

enter image description here

の構成をコンパイルし、次のとおりです。私は、次のサービスを作成しました

このエラーを修正するのに手伝ってください。

+0

は '.then()' 'return'を持っているべきですか?それがなければ、「仕事」がどのような価値を持っていると思いますか? – nnnnnn

+0

.then()は、これまでコールバックが完了したことを示すPromiseを返します。私はthen()のドキュメントでこれを見つけました –

+0

あなたが探している答えを見つけたことに感謝しますが、私の更新された答えはまだ投票に値しますか?それが間違っていれば、それは問題ありませんが、私はそれがだとは思わないし、下の投票は、後で問題を解決する可能性のある解決策として検討することを妨げるでしょう。ありがとう! –

答えて

3

あなたはjobsはタイプPromise<void>であることになり、あなたのthen内部に何かを返すされていません。 then内の配列を返します:

getCurrentJobs(): Promise<customType[]> { 
    var jobs = this.http.get(this.ordersUrl) 
     .toPromise() 
     .then(response => { 
     this.orders = response.json() as customType[]; 
     return this.orders; 
     }) 
     .catch(this.handleError); 
    return jobs; 
} 

は約束の連鎖行動を参照してください:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then#Chaining

-1

私は 'catch'演算子を追加しました。あなたのインターフェースへのアクセスをコード内のインターフェース定義のために入れ替えました。私はあなたのプロジェクトコードの残りの部分を除いてこれを実際にテストすることはできませんが、それは私には似ていて、VSCにエラーを投げかけません。

import { Injectable, OnInit } from '@angular/core'; 
import { Headers, Http } from '@angular/http'; 

import 'rxjs/add/operator/toPromise'; 
import 'rxjs/add/operator/catch'; 


export interface customType{ 
} 


@Injectable() 
export class JobService implements OnInit { 

    constructor(private http: Http) { } 
    private jobs: Promise<customType[]>; 

    ngOnInit(): void { 
     this.jobs = 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[]> { 
     return this.http.get(this.ordersUrl) 
      .map(response => response.json()) 
      .catch(this.handleError) 
      .toPromise(); 

    } 

    private handleError(error: any): Promise<any> { 
     console.error('An error occurred', error); 
     return Promise.reject(error.message || error); 
    } 
} 
関連する問題