在http.get中观察订阅

Jam*_*e J 2 storage observable typescript ionic2 angular

我在使用'MyAddressConfig'返回我的字符串时遇到了一些麻烦http.get.它从Ionic2 Storage获取数据.问题是我一直在努力

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

有任何想法吗?-谢谢

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/'
        });
    }
Run Code Online (Sandbox Code Playgroud)

为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); 
    }
Run Code Online (Sandbox Code Playgroud)

Rob*_*hof 6

MyConfig.GetRestAddress()没有返回一个字符串,它返回一个对象. [object%20object]是你得到的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); 
}
Run Code Online (Sandbox Code Playgroud)