[英]angular2 services returns undefined
我不明白為什么我的一項服務總是返回undefined
的原因,但是在開發人員模式下的“網絡”選項卡顯示我已接收到數據。
有人可以幫我弄清楚我的錯誤在哪里嗎?
我的組件看起來像這樣:
export interface Sponsors {
id;
sponsorCode;
sponsorName;
active;
clientId;
countryId;
cloudRegionId;
}
@Component({
selector: 'app-sponsors',
templateUrl: './sponsors.component.html',
styleUrls: ['./sponsors.component.css'],
})
export class SponsorsComponent implements OnInit {
id: any;
sponsors: Sponsors[];
applications: Applications[];
constructor(private appService: AppServices,
private route: ActivatedRoute) {
this.id = route.snapshot.url[1].path;
}
ngOnInit() {
this.getSponsors();
this.getApps();
}
getSponsors () {
this.appService.getSponsorsByClient(this.id).then(
response => {
this.sponsors = response;
console.log(this.sponsors) //returns undefined
});
}
getApps(){
this.appService.getApplications()
.then(applications => {
this.applications = applications;
console.log(this.applications) // returns data
});
}
這是我的服務:
getSponsorsByClient(id: any): Promise<Sponsors[]> {
const url = `${this.api}/clients/${id}/sponsors`;
return this.http.get(url)
.toPromise()
.then(response => response.json().data as Sponsors[])
}
getApplications(): Promise<Applications[]> {
return this.http
.get(this.api + "/applications")
.toPromise()
.then(response => response.json() as Applications[])
}
提前致謝
那應該工作:
getApplications(): Promise<Applications[]> {
return this.http.get(this.api + "/applications")
.map(response => response.json() as Applications[])
.toPromise();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.