Observables.map链不等待休息请求的回答

时间:2017-10-26 12:02:00

标签: angular rest rxjs reactivex

return this.billingRest.getAccount()
        .map((response: any) => {
            this.logger.debug('initAccountAndPlans - after getAcccount');
            this.logger.debug("getAccount response", response);
            this.logger.debug('getAccount teamID=' + teamId +
                ' info=' + response.account.firstName + ' ' + response.account.lastName);
            this.account = response.account;
            return response;
        })
        .map( () => {
            return this.billingRest.getPlans()
                .map( (data) => {
                    this.logger.debug('getPlans result', data);
                    let plans = data;
                    if (this.utilService.isDefined(vm.usePilotPlan) && vm.usePilotPlan) {
                        plans = [];
                        plans.push(this.getPilotPlan());
                    }
                    logger.debug('getPlans=' +  JSON.stringify(data));
                    const screenPlans = [];
                    for (let i = 0; i < plans.length; i++) {
                        const plan = plans[i];
                        const screenPlan = this.transformPlanToView(plan);
                        screenPlans.push(screenPlan);
                    };
                    if (screenPlans.length === 0) {
                        return Observable.throw({msg: 'cannot find plans'});
                    }
                    vm.planOptions = screenPlans;
                    this.logger.debug('currentTeam', this.sessionService.getCurrentTeam());
                    return this.billingRest.getCurrentPlan(teamId);
                }).map((response: any) => {
                    const currentPlan = response.plan;
                    this.logger.debug('currentPlan skuCode=' + currentPlan.sku);
                    vm.currentPlan = currentPlan;
                    const currentOrder = response.order;
                    vm.currentOrder = currentOrder;

                    vm.nextBillDate = this.getNextBillDate(currentOrder);
                    if (! this.utilService.isDefined(response.creditCard)) {
                        vm.currentPaymentMethod = {};
                    } else {
                        vm.currentPaymentMethod = response.creditCard;
                        this.logger.debug('currentPaymentMethod id=' + vm.currentPaymentMethod.uuid); // BraintreePaymentMethodId
                    }
                    this.logger.debug('currentPaymentMethod', vm.currentPaymentMethod);
                    vm.plan = this.getPlanById(this.planOptions, currentPlan); // for checkout
                    return Observable.throw({});
                })
                .catch((error) => {
                    console.log("COŚ TU JEST NIE TAK" + error);
                    this.logger.exception('init-getPlans : Ajax Error')(error);
                    return Observable.throw(error);
                }).subscribe();
        })
        .catch( (response) => {
            this.logger.exception('initAccountAndPlans : Ajax Error')(response);
            return observable.throw(response);
        });
}

这是我的代码。如您所见,我有一个主链和一个Observables.map运算符的子串。

时刻一切正常
    return this.billingRest.getCurrentPlan(teamId);
            }).map((response: any) => {
                const currentPlan = response.plan; 

它不会触发,下一个映射运算符不会等待响应,它会抛出错误'无法读取未定义的属性'。它的行为就像它没有订阅,但实际上它有。当我使用toPromise()转换获取CurrentPlan时,下一个映射不会等待结果,但函数起火了。我不知道怎么做,我不明白那个功能的行为。

  getCurrentPlan (teamId) {
        console.log('GET CURRENTPLAN PRZED RESTEM')
        return this.http.get(this.restUrl + this.plansPrefix + '/' + teamId + '/getCurrentPlan', {observe: 'response'})
            .map( (response) => {
                console.log(" FULFILLED LOG ");
                return response.body;
            })
            .catch((response) => {
                console.log('GETCURRENTPLANNIEDZIALA');
                return Observable.throw(response);
            });
    };

2 个答案:

答案 0 :(得分:0)

在这个块中

  return this.billingRest.getCurrentPlan(teamId);
}).map((response: any) => {
  const currentPlan = response.plan;

我没有看到 teamId 来自哪里。也许你的意思是this.teamId

所以也许,'无法读取未定义的属性'可能来自response.plan,因为响应未定义?

作为奖励,我想这个

const screenPlans = [];
for (let i = 0; i < plans.length; i++) {
  const plan = plans[i];
  const screenPlan = this.transformPlanToView(plan);
  screenPlans.push(screenPlan);
};

可以写成这个

const screenPlans = plans.map(plan => this.transformPlanToView(plan))

答案 1 :(得分:0)

initAccountAndPlans(vm){

    this.planOptions = [];
    this.plan = {}; // selected plan from planOptions
    this.currentPlan = {}; // currentPlan
    const teamId = this.sessionService.getCurrentTeam().uuid;

    /*          return billingRest.getPlans()
              .then(function(data) {
                console.log(data);
              })
              .catch(function(response) {
                console.log(response);
              })
     */
    return this.billingRest.getAccount()
        .map((response: any) => {
            this.logger.debug('initAccountAndPlans - after getAcccount');
            this.logger.debug("getAccount response", response);
            this.logger.debug('getAccount teamID=' + teamId +
                ' info=' + response.account.firstName + ' ' + response.account.lastName);
            this.account = response.account;
            return response;
        })
        .map( () => {
            return this.billingRest.getPlans()
                .map( (data) => {
                    this.logger.debug('getPlans result', data);
                    let plans = data;
                    if (this.utilService.isDefined(vm.usePilotPlan) && vm.usePilotPlan) {
                        plans = [];
                        plans.push(this.getPilotPlan());
                    }
                    logger.debug('getPlans=' +  JSON.stringify(data));
                    const screenPlans = [];
                    for (let i = 0; i < plans.length; i++) {
                        const plan = plans[i];
                        const screenPlan = this.transformPlanToView(plan);
                        screenPlans.push(screenPlan);
                    };
                    if (screenPlans.length === 0) {
                        return Observable.throw({msg: 'cannot find plans'});
                    }
                    vm.planOptions = screenPlans;
                    this.logger.debug('currentTeam', this.sessionService.getCurrentTeam());
                    return this.billingRest.getCurrentPlan(teamId);
                })
                .switchMap((response: any) => {
                    const currentPlan = response.plan;
                    this.logger.debug('currentPlan skuCode=' + currentPlan.sku);
                    vm.currentPlan = currentPlan;
                    const currentOrder = response.order;
                    vm.currentOrder = currentOrder;

                    vm.nextBillDate = this.getNextBillDate(currentOrder);
                    if (! this.utilService.isDefined(response.creditCard)) {
                        vm.currentPaymentMethod = {};
                    } else {
                        vm.currentPaymentMethod = response.creditCard;
                        this.logger.debug('currentPaymentMethod id=' + vm.currentPaymentMethod.uuid); // BraintreePaymentMethodId
                    }
                    this.logger.debug('currentPaymentMethod', vm.currentPaymentMethod);
                    vm.plan = this.getPlanById(this.planOptions, currentPlan); // for checkout
                    return Observable.throw({});
                })
                .catch((error) => {
                    console.log("COŚ TU JEST NIE TAK" + error);
                    this.logger.exception('init-getPlans : Ajax Error')(error);
                    return Observable.throw(error);
                }).subscribe();
        })
        .catch( (response) => {
            this.logger.exception('initAccountAndPlans : Ajax Error')(response);
            return observable.throw(response);
        });
}

这是一个完整的函数,teamId由sessionService.getCurrentTeam()。uuid编写。切换地图和合并地图不起作用。我不知道出了什么问题。