2017-05-09 1 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[]>'.* *

다음 얻을 비주얼 스튜디오 2017를 사용하여 코드를 컴파일 할 때 내 타이프 라이터가 Vs2017

enter image description here

의 구성을 컴파일 다음과 같습니다 나는 다음과 같은 서비스를 만들었습니다

이 오류를 해결할 수 있도록 도와주세요.

+0

을해야'그 때는()의''return'이? 그것이 없다면,'직업'이 어떤 가치를 부여 받았다고 생각합니까? – nnnnnn

+0

.then()는 콜백이 완료 될 때까지 Promise를 반환합니다. 나는() –

+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); 
    } 
} 
관련 문제