为什么我的代码在subscribe(observables)之后不执行?

时间:2018-06-20 11:14:26

标签: angular typescript ionic-framework observable

我有2种方法可以获得2个列表(接受的用户和收藏的用户),代码的第一部分运行良好,并返回接受和收藏的用户,但是在代码的第二部分中,我想清除接受的用户列表如果用户在“收藏的用户”列表中。

下一个是我在组件中的代码:

    ionViewWillEnter() {
//FIRST
          this._cp.listContacts('accepted').subscribe((invitations: UserProfile[]) => {
                console.log("EN ACCEPTED");
                this.invitationsAccepted = invitations;
            }, error => {
                console.log(<any>error);
            })
            this._cp.listContacts('favorites').subscribe((invitations: UserProfile[]) => {
                console.log("EN FAV");
                this.favorites = invitations;
            }, error => {
                console.log(<any>error);
            })
    //SECOND
         console.log("LENG INVITATION ACCEPTED"+this.invitationsAccepted.length);
                console.log("LENG INVITATION Favorites"+this.favorites.length);

                this.invitationsAccepted = this.invitationsAccepted.filter(o1 => this.favorites.some(o2 => o1.id != o2.id));
                console.log("RESULT");
                console.log(this.invitationsAccepted)
    }

有时它起作用而某些例程不起作用,当完成第一部分时,我如何强制依次执行第二部分?

我也尝试过用forkjoin做到这一点:

         let listAccepted=this._cp.listContacts('accepted');
         let favorites=this._cp.listContacts('favorites');

         forkJoin([listAccepted,favorites]).subscribe(results=>{               
             this.invitationsAccepted=results[0];
             this.favorites=results[1];
             console.log(results[0]);
             console.log(results[1]);
             this.invitationsAccepted = this.invitationsAccepted.filter(o1 => this.favorites.some(o2 => o1.id != o2.id));
         },error=>{
             console.log(<any>error);
         })

问题在于,当listAccepted和收藏夹没有错误时,换句话说,当两者都包含数据时,它运行良好,因为后端没有数据时,我的后端返回错误。

我希望你能帮助我。

谢谢。

3 个答案:

答案 0 :(得分:0)

您的代码应按顺序执行。

    ionViewWillEnter() {
    //FIRST
              this._cp.listContacts('accepted').subscribe((invitations: UserProfile[]) => {
                    console.log("EN ACCEPTED");
                    this.invitationsAccepted = invitations;

                    this._cp.listContacts('favorites').subscribe((invitations: UserProfile[]) => {
                       console.log("EN FAV");
                       this.favorites = invitations;

                       this.invitationsAccepted = this.invitationsAccepted.filter(o1 => this.favorites.some(o2 => o1.id != o2.id));
                       }, error => {
                         console.log(<any>error);
                       })
                }, error => {
                    console.log(<any>error);
                })
        }

答案 1 :(得分:0)

无需嵌套服务调用即可实现目标的最简单方法是:

private acceptedReceived: boolean = false;
private favoritesReceived: boolean = false;
private favoritesError: boolean = false;

ionViewWillEnter() {
      //FIRST
      this._cp.listContacts('accepted').subscribe((invitations: UserProfile[]) => {
            console.log("EN ACCEPTED");
            this.invitationsAccepted = invitations;
            this.acceptedReceived = true;
            this.filterUsers();
      }, error => {
            console.log(<any>error);
      });

      this._cp.listContacts('favorites').subscribe((invitations: UserProfile[]) => {
            console.log("EN FAV");
            this.favorites = invitations;
            this.favoritesReceived = true;
            this.filterUsers();
      }, error => {
            console.log(<any>error);
            this.favoritesError = true;
            this.filterUsers();
      });  
}

private filterUsers(): void {
    if(this.acceptedReceived && (this.favoritesReceived || this.favoritesError)){
        //SECOND
        console.log("LENG INVITATION ACCEPTED"+this.invitationsAccepted.length);
        console.log("LENG INVITATION Favorites"+this.favorites.length);

        this.invitationsAccepted = this.invitationsAccepted.filter(o1 => this.favorites.some(o2 => o1.id != o2.id));

        console.log("RESULT");
        console.log(this.invitationsAccepted) 
    }

}

答案 2 :(得分:0)

subscribe是异步的,两个事件可能不会一个接一个地给出结果。尝试像这样在this._cp.listContacts('favorites')的成功回调中调用this._cp.listContacts('accepted'),您可以确定设置了this.invitationsAccepted的值,并在this._cp.listContacts('favorites')的成功回调中可以按需过滤用户希望。 喜欢

this._cp.listContacts('accepted').subscribe((invitations: UserProfile[]) => {
                console.log("EN ACCEPTED");
                this.invitationsAccepted = invitations;
                this._cp.listContacts('favorites').subscribe((favs: UserProfile[]) => {
                console.log("EN FAV");
                this.favorites = favs;
                this.invitationsAccepted = this.invitationsAccepted.filter(o1 => this.favorites.some(o2 => o1.id != o2.id));
            }, err => {
                console.log(<any>err);
            })
            }, error => {
                console.log(<any>error);
            })