2017-03-22 12 views
0

私のサービスの1つがいつもundefinedを返す理由はわかりませんが、開発者モードのネットワークタブではデータが受信されたことがわかります。angular2サービスは未定義を返す

誰かが私のミスがどこにあるのか理解してもらえますか?

+1

'.then(

私のコンポーネントは、次の方法を探しますresponse => response.json()。スポンサーとしてのデータ[]) 'this d oesは '.data'を持っていますが、別の方法はありません。これが理由だろうか? –

+1

@ YaroslavAdmin愚かなこと、それは私の問題でした。通知ありがとう! – antonyboom

答えて

0

が動作するはず事前に

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[]) 
    } 

ありがとう::

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

これは私のサービス:

getApplications(): Promise<Applications[]> { 
    return this.http.get(this.api + "/applications") 
       .map(response => response.json() as Applications[]) 
       .toPromise(); 
} 
関連する問題