RxJs - 如何连锁经营

时间:2017-07-12 21:05:33

标签: angular rxjs

this.api.one('profiles',ui.sub).get()下面的代码中的内部http调用没有执行,我不知道为什么。我期望,因为内部地图调用返回一个Observable并被flatMap包装,我只需要在结束时调用subscribe()一次,但看起来我错了。

this.$http.get(uiUri, {headers: new Headers({'Authorization': 'Bearer ' + user.access_token})})
    .map(resp => resp.json())
    .flatMap((ui: IID4UserInfo) => {
        let partialAccount = new Account(ui);
        partialAccount.isLoggedIn = false;
        this.currentAccount = partialAccount;
        return this.api.one('profiles', ui.sub).get().map((profile) => {
            this.currentAccount = new Account(profile.plain()); //never executes
            this.storage.set('accountService.localAccount', this.currentAccount);
            this.currentAccount.isLoggedIn = true;
            this.accountSource.next(this.currentAccount);
            if (this.loader)
                this.loader.dismiss();
            return this.currentAccount;
        }, (err) => {
            this.log.error(err);
            this.currentAccount = partialAccount;
            this.accountSource.next(partialAccount);
            if (this.loader)
                this.loader.dismiss();
            return this.currentAccount;
        })
    })
    .subscribe((profile) => {
        this.log.info('user fully loaded');
    }, err => {
        this.log.error(err)
    });

FYI this.api.one('profiles',ui.sub).get()解析为:

get(queryParams?: string, headers?: any): Observable<IElement> {
    let path = queryParams ? this.currentPath + '?' + queryParams : this.currentPath;
    return this.api.http.get(path, {headers: this['_getHeaders'](headers)}).map(r => r.json()).map(resp => {
        return Object.assign(new Element(this.api, this.currentPath), resp);
    });
}

1 个答案:

答案 0 :(得分:1)

我认为你不能在.map()函数中传入错误回调。改为使用.catch()

this.$http.get(uiUri, {headers: new Headers({'Authorization': 'Bearer ' + user.access_token})})
    .map(resp => resp.json())
    .flatMap((ui: IID4UserInfo) => {
        let partialAccount = new Account(ui);
        partialAccount.isLoggedIn = false;
        this.currentAccount = partialAccount;
        return this.api.one('profiles', ui.sub)
            .get()
            .map((profile) => {
                this.currentAccount = new Account(profile.plain()); //never executes
                this.storage.set('accountService.localAccount', this.currentAccount);
                this.currentAccount.isLoggedIn = true;
                this.accountSource.next(this.currentAccount);
                if (this.loader)
                    this.loader.dismiss();
                return this.currentAccount;
            })
            //use a catch block
            .catch((err) => {
                this.log.error(err);
                this.currentAccount = partialAccount;
                this.accountSource.next(partialAccount);
                if (this.loader)
                    this.loader.dismiss();
                return this.currentAccount;
            })
    })
    .subscribe((profile) => {
        this.log.info('user fully loaded');
    }, err => {
        this.log.error(err)
    });