The type 'Promise <string []>' is not assigned to the type 'string []'
Getting the following error: The type 'Promise' is not assigned to the type 'string []'. The 'includes' property is not in the 'Promise' type.
when I use Promise to enter 'string []' My code is below,
Component: app.dashboard.ts
import {Component} from '@angular/core';
import { MemberService } from "./app.service";
@Component({
selector:'app-root',
templateUrl:'./app.dashboard.html',
providers:[MemberService]
})
export class AppDashboard{
title='Dashboard'
constructor(private memberService: MemberService) { }
public doughnutChartLabels:string[] =
this.memberService.getmemberheader();//error occurred here
}
}
Services: app.service.ts
import { Injectable } from '@angular/core';
import { Member } from './Member';
import { Http, Response, Headers, RequestOptions, URLSearchParams } from'@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class MemberService
{
constructor(private http: Http) {
}
private getHeaders(){
// I included these headers because otherwise FireFox
// will request text/html instead of application/json
let headers = new Headers();
headers.append('Accept', 'application/json');
return headers;
}
getmemberheader(): Promise<string[]> {
return this.http
.get(`/ReportService/MemberDatabaseCountryname`, {headers: this.getHeaders()})
.toPromise()
.then(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body || {};
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
}
+9
2 answers
Suppose your answer from http.getis an array, here you are returning Promisefrom a function memberService.getmemberheader, you should get the result of the promise when you call it back then(without assigning yourself to the array itself doughnutChartLabels).
public doughnutChartLabels: string[];
this.memberService.getmemberheader().then(res => {
this.doughnutChartLabels = res;
})
+10
@levolutionniste
getmemberheader() .
Async/Callback. ES6 Promise. , , .
:
getmemberheader()
.then((returnsAStringArrayObjectHere_YouCanCallItAnythingYouLike) => {
// Do something with this array here
})
.catch((error) => {
console.log("Oops something went wrong: ", error);
});
:
- https://basarat.gitbooks.io/typescript/docs/promise.html - .
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
getmemberheader string [].
this.memberService.getmemberheader().then(res => {
this.doughnutChartLabels = res;
})
res [] getmemberhead. {} doughnutChartLabels res.
, ORM, , , .
.
0