Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'void' is not assignable to type 'any[]'

I am getting error at following line of the constructor code

this.data = this.getRisks(); . 

The error is

Type 'void' is not assignable to type 'any[]'.

Could somebody tell me how do i assign it .

import { Component, OnInit } from '@angular/core';
import { Risk } from './risk';
import { RiskService } from './risk.service';
import { GridModule, GridDataResult, PageChangeEvent } from '@progress/kendo-angular-grid';

@Component({
    moduleId: module.id,
    selector: 'rm-risks',
    templateUrl: '/app/risk-list.component.html',
    providers: [RiskService]
})

export class RiskListComponent implements OnInit {
    private gridView: GridDataResult;
    private data: any[];
    private pageSize: number = 50;
    private skip: number = 0;
    title = 'Risk List';
    risks: Risk[];

    constructor(private _riskService: RiskService) {
        this.data = this.getRisks();
        this.loadRisks();
     }

    protected pageChange(event: PageChangeEvent): void {
        this.skip = event.skip;
        this.loadRisks();
    }

    private loadRisks(): void {
        this.gridView = {
            data: this.data.slice(this.skip, this.skip + this.pageSize),
            total: this.data.length
        };
    }

    getRisks(): void {
        this._riskService.getRisks().then(risks => this.risks = risks);

    }

    ngOnInit(): void {
        this.getRisks();
    }
};

risk.service.ts

import { Injectable } from '@angular/core';
import { Risk } from './risk';
import { Risks } from './mock-risk';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/from';


@Injectable()
export class RiskService {

    getRisks(): Promise<Risk[]> {
        return Promise.resolve(Risks);
    }

}
like image 691
Tom Avatar asked Oct 18 '16 21:10

Tom


1 Answers

Since getRisks() doesn't return anything, simply call it and it will set this.data

constructor(private _riskService: RiskService) {
    this.getRisks();
    this.loadRisks();
 }

You can set this.data = [] if you need it to be defined before data is available.

like image 57
Sasxa Avatar answered Oct 11 '22 10:10

Sasxa