【发布时间】:2020-03-06 17:05:53
【问题描述】:
你能告诉我问题是否出在这段代码中。我正在尝试设置数据轮询。对第一个请求的响应之后的响应不会传输到后端,并且我返回的 observables 都具有与第一个响应相同的值。即使后端的状态发生变化也是如此。
示例如果第一个 getStatus 返回打开,则不会传输以下请求并且将打开可观察的响应
在我的服务中,我有以下代码
getIntervalStatus(sessionId: string): Observable<HttpResponse<string>> {
return this.getStatus(sessionId).pipe(
map(
(result: HttpResponse<string>) => {
this.statusOpened = result.body.toLowerCase();
this.utils.alertLog(this.getStatus, result.body.toLowerCase());
this.statusEmit.next(this.statusOpened);
if (this.statusOpened === 'opened' || this.statusOpened === 'closed') {
this.getIntervalStatus(sessionId).subscribe( );
}
return result;
}
),
catchError((err) => {
this.statusOpened = 'error';
this.utils.alertLog(this.getStatus, JSON.stringify(err));
this.statusEmit.error(err);
throw err;
})
);
}
getStatus(sessionId: string): Observable<HttpResponse<string>> {
const httpParams = new HttpParams().set('sessionId', sessionId);
return this.http.post<string>(this.fullWsUrl + '/Payment/GetPaymentStatus', null,
{ observe: 'response', params: httpParams }).pipe(
map((value: HttpResponse<string>) => {
this.utils.alertLog(this.getStatus, value.status.toString());
return value;
})
);
}
在我的组件中,我有以下代码
this.openSessionSubscription = this.device.openSession(amount, this.printerVMService.getSessionId()).subscribe(
data => {
this.utils.alertLog(this.device.getIntervalStatus, 'Session is opened');
this.sessionIsOpened = true;
// subject for information session is opened
this.device.statusOpened = 'opened';
this.device.statusEmit = new Subject<string>();
this.statusEmitSubscriber = this.device.statusEmit.subscribe(
status => {
this.status = status;
console.log(this.constructor.name + 'payment status', this.status);
switch (this.status) {
case 'opened':
break;
case 'payed':
this.utils.alertLog(this.device.getIntervalStatus, 'statut payed');
this.localStorageService.storePaymentIsMade(true);
this.dbService.addOrderPaymentResult(ProcessResult.done, this.printerVMService.getSessionId()).subscribe();
this.router.navigate(['welcome/paying/paying_accepted']);
break;
case 'closed':
this.utils.alertLog(this.device.getIntervalStatus, 'statut closed');
// this.paymentSessionIsClosed = true;
// this.dbService.addOrderPaymentResult(ProcessResult.error, this.printerVMService.getSessionId()).subscribe();
// this.utils.deleteSectionAction(this.printerVMService.getPrinterVM());
break;
case 'used':
this.utils.alertLog(this.device.getIntervalStatus, 'status used');
// this.utils.deleteSectionAction(this.printerVMService.getPrinterVM());
break;
default:
console.error('status don\'t exist');
this.utils.alertLog(this.device.getIntervalStatus, 'statut not exist');
this.utils.deleteSectionAction(this.printerVMService.getPrinterVM());
}
},
error => {
this.alert.success('statut error');
console.error(this.translate.instant('PAYMENT.DEVICE.GETSTATUSERRORMSG'));
this.utils.deleteSectionAction(this.printerVMService.getPrinterVM());
}
);
this.utils.alertLog(this.device.getIntervalStatus, 'getIntervalStatusSubscription is call');
this.getIntervalStatusSubscription = this.device.getIntervalStatus(this.printerVMService.getSessionId()).subscribe();
},
error => {
// emit session not open
console.error(this.translate.instant('PAYMENT.DEVICE.OPENERRORMSG'));
this.utils.alertLog(this.device.openSession, 'open error');
this.utils.deleteSectionAction(this.printerVMService.getPrinterVM());
}
);
【问题讨论】:
-
我指定只有 iphone 才有这个问题
-
你能在 stackblitz 中使用 minimal 代码重新创建它吗?你可以在 iPhone 上测试一个 stackblitz 来检查。
-
另外,
this.utils.alertLog在做什么? -
this.utils.alertLog 允许您制作弹出窗口以显示返回值。用于调试
-
你能告诉我这是否是使用 Angular 编写池数据的最佳方式吗??