[英]Observable Subscribe in http.get
Im have a bit of trouble getting 'MyAddressConfig' to return a string in my http.get
. 我在让'MyAddressConfig'返回我的
http.get
的字符串时遇到了一些麻烦。 It gets the data from Ionic2 Storage. 它从Ionic2 Storage获取数据。 The problem is that I keep getting
问题是我不断
GET http://localhost:0000/[object%20Object]my/path?&tst=1 404 (Not Found)
GET http:// localhost:0000 / [object%20Object] my / path?&tst = 1 404(未找到)
Any ideas? 有任何想法吗? -thanks
-谢谢
MyAddressConfig 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 为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);
}
Your MyConfig.GetRestAddress()
does not return a string, it return an object. 您的
MyConfig.GetRestAddress()
不返回字符串,而是返回一个对象。 [object%20object]
is what you get from MyConfig.GetRestAddress() because your object is parsed to a string
[object%20object]
是您从MyConfig.GetRestAddress() because your object is parsed to a string
获得的内容, MyConfig.GetRestAddress() because your object is parsed to a string
This is because GetRestAddress()
return a subscription. 这是因为
GetRestAddress()
返回预订。 Something like this is what you want: 您需要这样的东西:
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.