angular2服务返回未定义

编程入门 行业动态 更新时间:2024-10-27 12:24:03
本文介绍了angular2服务返回未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我无法得到我的一项服务始终返回undefined的原因,但是在开发人员模式下的网络"选项卡显示我已接收到数据.

I cannot get the reason why one of my services always returns undefined however network tab in developer mode shows me that data has been received.

有人可以帮我弄清楚我的错误在哪里吗?

Could somebody help me to figure out where my mistake is?

我的组件看上去像这样:

My component looks next way:

export interface Sponsors { id; sponsorCode; sponsorName; active; clientId; countryId; cloudRegionId; } @Component({ selector: 'app-sponsors', templateUrl: './sponsorsponent.html', styleUrls: ['./sponsorsponent.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(); }

更多推荐

angular2服务返回未定义

本文发布于:2023-11-22 23:14:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619294.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未定义

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!