繁体   English   中英

在http.get中可观察的订阅

[英]Observable Subscribe in http.get

我在让'MyAddressConfig'返回我的http.get的字符串时遇到了一些麻烦。 它从Ionic2 Storage获取数据。 问题是我不断

GET http:// localhost:0000 / [object%20Object] my / path?&tst = 1 404(未找到)

有任何想法吗? -谢谢

MyAddressConfig

GetDataFromStorage: Observable<any> =

Observable.fromPromise(
    Promise.all([
        this.ionicStorage_.get('MyRestIPAddress'), // 'localhost'
        this.ionicStorage_.get('MyRestIPPort'), // '0000'
    ])
        .then(([val1, val2]) => {
            this.MyRestIPAddress = val1;
            this.MyIPPort = val2;
            return [val1, val2];
        })
);

 GetRestAddress() {
        return this.GetDataFromStorage.subscribe(([val1, val2]) => { // 'localhost','0000'
           let RestAddress = 'http://' + val1 + ':' + val2 + '/rest/';
           console.log(RestAddress);
           return RestAddress;  // 'http://localhost:0000/rest/'
        });
    }

为MyService

getStoresSummaryResults(): Observable<MyTypeClass> {
        let MyConfig: MyAddressConfig;
        MyConfig = new MyAddressConfig(this.ionicStorage_);

        return this.http_.get(MyConfig.GetRestAddress() + 'my/path?&tst=1')
            .map(res => res.json()) 
            .catch(this.handleError); 
    }

您的MyConfig.GetRestAddress()不返回字符串,而是返回一个对象。 [object%20object]是您从MyConfig.GetRestAddress() because your object is parsed to a string获得的内容, MyConfig.GetRestAddress() because your object is parsed to a string

这是因为GetRestAddress()返回预订。 您需要这样的东西:

GetRestAddress() { //return the url as Observable
    return this.GetDataFromStorage.switchMap(([val1, val2]) => { 
       let RestAddress = 'http://' + val1 + ':' + val2 + '/rest/';
       return Observable.of(RestAddress);  // 'http://localhost:0000/rest/'
    });
}


getStoresSummaryResults(): Observable<MyTypeClass> {
    let MyConfig: MyAddressConfig;
    MyConfig = new MyAddressConfig(this.ionicStorage_);

    return MyConfig.GetRestAddress()
        .switchMap(url => this.http_.get(url + 'my/path?&tst=1')
        .map(res => res.json()) 
        .catch(this.handleError); 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM