Angular2 / 4 http.get嵌套访问"这个"

时间:2017-08-11 06:19:01

标签: angular typescript

我有这段代码

 let headers = new Headers({ 'Authorization': `${this.user.Token}` });
        let options = new RequestOptions({ headers: headers });
        this.http.get(environment.apiRoot + 'Customers/Customer/Search/' + term + '/' + value, options)
            .map((response) => {
                var Data = response.json().Data;
                return Data;
            }).subscribe(Data => {
                this.data.customer.emailAddress = Data.EmailAddress;
                this.data.customer.phoneNumber = Data.PhoneNumber;
                this.data.customer.firstName = Data.FirstName;
                this.data.customer.lastName = Data.LastName;
                this.data.customer.streetAddress = Data.StreetAddress;
                this.data.customer.city = Data.City;
                this.data.customer.state = Data.State;
                this.data.customer.zipCode = Data.ZipCode;
                this.data.customer.idNumber = Data.IDNumber;
                this.data.customer.ID = Data.ID;
                this.data.customer.comment = Data.Comment;

                // //Check if customer has exsiting orders
                this.http.request(environment.apiRoot + 'Orders/Order/Customer/' + Data.ID, options)
                    .subscribe(odResponse => {
                        var Data = odResponse.json().Data;
                        this.data.orders = Data;
                        this.existingOrders = Data.length > 0;
                    }, error => {
                        this.alertService.error('Encountered an error while trying to get customer order data');
                    });
            }, error => {
                this.alertService.error('Encountered an error while trying to get customer data');
            });

我遇到的问题是第一次" GET"完成了" this.data.customer"设置,即使在调试器中,这个"显示为订阅而不是我的组件,然后当第二个" GET"执行"这"是订阅和说" this.data.order"是未定义的,所以" this.data"

所以我想问题是我如何得到"这个"在我订阅是组件而不是订阅。

1 个答案:

答案 0 :(得分:0)

正如Harry建议的那样,最好使用switchMap并在订阅中执行一次订阅调用而不是调用。关于解决方案,请尝试此

let headers = new Headers({ 'Authorization': `${this.user.Token}` });
let options = new RequestOptions({ headers: headers });

this.http.get(environment.apiRoot + 'Customers/Customer/Search/' + term + '/' + value, options)
        .map((response) => { // This extracts the data you require
            var Data = response.json().Data;

            return Data;
        }).switchMap(Data => { // This returns the result of the second observable in 
            return this.http.request(environment.apiRoot + 'Orders/Order/Customer/' + Data.ID, options)
                            .map(odResponse => { // We have the response. Extract the json, and return an array with the first element as the old data, and the second element as the new data.
                                 var _Data = odResponse.json().Data;
                                 return [ Data, _Data ];
                             });
        }).subscribe( data => {
            let firstSet = data[0];
            let secondSet = data[1];
            // Now do your assignment 
            this.data.customer.emailAddress = firstSet.EmailAddress;
            this.data.customer.phoneNumber = firstSet.PhoneNumber;
            ...
            ...
        });

我使用过这种方法,因为您似乎需要第一次调用返回的数据(ID)才能进行第二次调用。如果不是这样,我会建议使用Observable.forkJoin。 This是RxJS V4中forkJoin的文档,但它仍然适用于V5。