我不认识伙计们,但这有什么问题?
this.auth.userAvailable(data.email)
.flatMap(res => {
return Observable.if(
function () {
return res.success;
},
Observable.fromPromise(this.auth.localStore('user_info', data)),
Observable.throw(new Error('User exists'))
)
})
.subscribe(res => {
this.navCtrl.push(ServiceProviderBusinessInfoPage);
}, err => {
let error = err.json();
this.alertCtrl.create({
title: 'Error',
message: error.message,
buttons: ['OK']
}).present();
});
即使每次res.success
被解雇时都会返回false
this.auth.localStore('user_info', data)
, 此外,订阅成功呼叫未被触发
帮我写这段代码,我是 rxjs 的新手
在您的情况下,实际上并不需要Observable.if
,为什么不使用一个简单的switchMap
:
this.auth.userAvailable(data.email)
.switchMap(res => {
if (res.success) {
return Observable.fromPromise(this.auth.localStore('user_info', data));
} else {
return Observable.throw(new Error('User exists'));
}
})
.subscribe(res => {
this.navCtrl.push(ServiceProviderBusinessInfoPage);
}, err => {
let error = err.json();
this.alertCtrl.create({
title: 'Error',
message: error.message,
buttons: ['OK']
}).present();
});
这与if
的工作方式有关。这是在这里报告的:https://github.com/ReactiveX/rxjs/issues/2768
解决方法放在这里:https://github.com/ReactiveX/rxjs/issues/2768#issuecomment-317877914
因此,应用于您的示例,它将是:
this.auth.userAvailable(data.email)
.switchMap(res => {
return Observable.if(
() => res.success,
Observable.defer(() => Observable.fromPromise(this.auth.localStore('user_info', data))),
Observable.throw(new Error('User exists'))
)
})
.subscribe(res => {
...