【发布时间】:2020-05-13 06:45:54
【问题描述】:
我正在尝试合并三个可观察对象,当内部可观察对象没有任何数据时,mergeMap 不会返回任何数据。即使内部可观察对象之一为空,我也希望能够继续该过程。我该如何处理这种情况?这是我的代码:
ngOnInit() {
this.accountStatusSub = this.accountService.accountNumberChange$.subscribe(
accNumber =>
{this.accountNumber = accNumber;
var obs = this.accountService.getAccountDetails(this.accountNumber)
.pipe(mergeMap(accountData =>
this.accountService.getBill(accountData.account[0].accountNumber)
.pipe(mergeMap(billData =>
this.accountService.getPayment(accountData.account[0].accountNumber)
.pipe(map(paymentData => ({
address1: accountData.account[0].address1,
address2: accountData.account[0].address2,
city: accountData.account[0].city,
state: accountData.account[0].state,
zip: accountData.account[0].zip,
amountDue: billData.bill[0].amountDue,
dueDate: billData.bill[0].dueDate,
lastPaymentAmount: paymentData.payment[0].paymentAmount,
lastPaymentDate: paymentData.payment[0].paymentDate
})
))
))
))
obs.subscribe(combinedAccountData => {
console.log('MergeMap:', combinedAccountData)
})
})
}
当 billData 或 paymentData 为空时,combinedAccountData 为空。有没有更好的方法来编写上面的代码?我是 Angular 和 rxjs 的新手。谢谢。
【问题讨论】:
-
既然你可以从一开始就知道obs.是否为空,是否会有超时声明它为空?
-
您能详细说明一下吗?我可以在代码中的哪个位置执行此操作?我是 Angular 新手,但仍在尝试弄清楚。
-
所以你说一个 observable 可以是空的,但是你怎么知道它是空的并且不会比预期的时间长一点?它是否必须发射到一定的时间,否则它被认为是空的?另外,如果一个 obs 为空,“继续该过程”是什么意思?预期的行为是什么?
-
它现在的工作方式是按照上面的代码,例如,如果 api 调用没有返回 paymentData 行,则没有为 accountData 或 billData 设置值。即使 paymentData 或 billData(getBill 或 getPayment)不返回任何行,我也希望能够检索其余字段(如 address1、账单数据等)的值。