【问题标题】:RxJs - How to chain operationsRxJs - 如何链接操作
【发布时间】:2017-07-12 21:05:33
【问题描述】:

this.api.one('profiles', ui.sub).get() 下面代码中的内部 http 调用没有执行,我不知道为什么。我希望因为内部 map 调用返回一个 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)
    });

仅供参考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);
    });
}

【问题讨论】:

    标签: angular rxjs


    【解决方案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)
        });
    

    【讨论】:

    • 谢谢舒适。我已经更新了我的代码,但不幸的是它并没有解决问题。 http 调用仍然没有出去。
    • @Justin 是 profile.plain() 也是异步调用吗?
    • 它正在工作。在 GET 请求之前发送的 OPTIONS 请求超时,直到我再次在代理下运行它才捕获它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    相关资源
    最近更新 更多