admin管理员组

文章数量:1201201

I am new to angularJs2. I have created following service:

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);
    }
}

Following are my Typescript compile configuration of Vs2017

When I compile the code using visual studio 2017 I get following error

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

Help me to fix this error.

I am new to angularJs2. I have created following service:

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);
    }
}

Following are my Typescript compile configuration of Vs2017

When I compile the code using visual studio 2017 I get following error

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

Help me to fix this error.

Share Improve this question edited May 9, 2017 at 4:07 nnnnnn 150k30 gold badges208 silver badges247 bronze badges asked May 9, 2017 at 3:52 MARKAND BhattMARKAND Bhatt 2,63011 gold badges51 silver badges86 bronze badges 3
  • Should the .then() have a return? Without it, what value do you think jobs is being assigned? – nnnnnn Commented May 9, 2017 at 4:09
  • .then() returns a Promise for the completion of which ever callback is executed. I found this in the documentation of then() – MARKAND Bhatt Commented May 9, 2017 at 4:22
  • I appreciate that you've found the answer you're looking for, but does my updated answer still deserve a down vote? If it's incorrect then that's fine, but I don't think it is, and a down vote will prevent someone finding this later from considering it as a possible solution to their problem. Thanks! – Stephen R. Smith Commented May 10, 2017 at 2:02
Add a comment  | 

2 Answers 2

Reset to default 16

You are not returning anything inside your then which makes jobs be of type Promise<void>. Return the array inside 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;
}

See the chaining behaviour of promises: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then#Chaining

I've added the 'catch' operator, and swapped your interface import for an interface definition in the code (as I don't obviously have access to yours). I can't really test this without the rest of your project code, but it looks right to me and doesn't throw any errors 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);
    }
}

本文标签: javascriptType Promiseltvoidgt is not assignable to type PromiseltcustomTypegtStack Overflow