订阅返回Observable

时间:2017-05-30 03:21:43

标签: firebase ionic2 lodash angular2-services angular2-observables

我正在订阅服务以获得一组团队

teamService代码:

这是我的teamService代码(从firebase获取结果):

getTeams(): Observable<Team[]> {
    this.login();
    let teams: Team[] = [];
    return this._db.list('teams')
      .map((response) => {
        response.forEach(team => {
          this._fb.storage().ref().child('teams/' + team.photo).getDownloadURL()
            .then(imageUrl => {
              teams.push(new Team(team.id, team.name, imageUrl, team.zone, team.points, team.matchesPlayed,
                team.matchesWon, team.matchesDrawn, team.matchesLost, team.goalsScored,
                team.goalsReceived, team.dif));
            })
            .catch(error => {
              console.log(error);
            });
        });
        return teams;
      });
  }

我在这里订阅我的服务以获得团队:

loadStandings() {
    let loading = this.loadingCtrl.create({
      spinner: 'crescent'
    });
    loading.present();
    this.teamService.getTeams().subscribe(t => {
      this.teams = t;
      console.log('showing this.teams');
      console.log(this.teams); // this.teams declared as any[]
      this.teamsOrdered = _.orderBy(this.teams, ['points', 'dif'], ['desc', 'desc']);
      console.log('showing this.teamsOrdered');
      console.log(this.teamsOrdered); // this.teamsOrdered declared as any[]
    });
    loading.dismiss();
  }

console.log(this.teams)向我展示了服务返回的预期数组,但console.log(this.teamsOrdered)为空。

Lodash导入import * as _ from 'lodash';位于我的ts文件顶部

此处来自控制台的屏幕截图

console output

编辑:我在调试时添加了我的getTeamsService代码(在console.log(this.teams);上放置一个断点我意识到在从服务调用中检索值之前,this.teams=t下面的代码正在执行。 如果我不进行调试,那么结果与我的控制台输出屏幕截图相同。

我做错了什么?

0 个答案:

没有答案
相关问题