2017-05-09 2 views
0

Ich bin neu bei angularJs2. Ich habe folgende Service erstellt:Typ Promise <void> ist nicht zuweisbar Typ 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); 
    } 
} 

Im Folgenden sind meine Typoskript Konfiguration Vs2017 kompilieren

enter image description here

Wenn ich den Code mit Visual Studio kompilieren 2017 bekomme ich folgende Fehler

**TS2322 Build:Type 'Promise<void>' is not assignable to type 'Promise<customType[]>'.* *

Helfen Sie mir, diesen Fehler zu beheben.

+0

Sollte der '.then()' 'eine return'? Ohne welchen Wert werden Ihrer Meinung nach Jobs zugewiesen? – nnnnnn

+0

.then() gibt ein Promise zurück, mit dessen Hilfe der Rückruf ausgeführt wird. Ich fand das in der Dokumentation von dann() –

+0

Ich weiß zu schätzen, dass Sie die Antwort gefunden haben, die Sie suchen, aber hat meine aktualisierte Antwort immer noch eine Down-Abstimmung verdient? Wenn es falsch ist, dann ist das in Ordnung, aber ich glaube nicht, dass es das ist, und eine Abstimmungsentscheidung wird verhindern, dass jemand dies später als mögliche Lösung für ihr Problem betrachtet. Vielen Dank! –

Antwort

3

Sie sind nicht in Ihrem then Rückkehr alles, was jobs von Promise<void> Typ macht. Bringen Sie das Array innerhalb 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; 
} 

Siehe Verkettungsverhalten verspricht: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then#Chaining

-1

Ich habe den Operator 'catch' hinzugefügt und den Schnittstellenimport für eine Schnittstellendefinition im Code getauscht (da ich offensichtlich keinen Zugriff auf Ihren habe). Ich kann das nicht ohne den Rest Ihres Projektcodes testen, aber es sieht gut aus und wirft keine Fehler in 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); 
    } 
} 
Verwandte Themen