如果响应不为空,Angular只运行forkJoin ?



我有一个获取通知规则列表的函数。问题是,如果不存在通知规则,则函数崩溃。引起问题的行是response.rules.model.map((rule) => {返回错误:

ERROR TypeError: Cannot read property 'map' of undefined

public getNotificationRuleItemsSet(): Observable<any[]> {
const commonTxnList = this.configService.config.Notifications.TransactionTypeRules
.CommonTxnList;
const excludeTxnTypes = this.configService.config.Notifications.TransactionTypeRules
.ExcludeTxnTypes;
const showEffectForTxnTypes = this.configService.config.Notifications.TransactionTypeRules
.ShowEffectForTxnTypes;
return this.getNotificationRules().pipe(
take(1),
tap((response: any) => {
if (response.txntypes) {
this.txnTypes = response.txntypes.model;
}
if (response.accounts) {
this.accounts = response.accounts.model;
}
}),
switchMap((response: any) => {
return forkJoin([
forkJoin(
response.txntypes.model.map((txntype) => {
let commonIndex = commonTxnList.indexOf(txntype.txntypename);
let excludeIndex = excludeTxnTypes.indexOf(txntype.txntypename);
let showEffectIndex = showEffectForTxnTypes.indexOf(
txntype.txntypename
);
let item: INotificationTxnType = {
id: txntype.id,
txntypename: txntype.txntypename,
locname: txntype.locname,
shortList: commonIndex >= 0 ? true : false,
exclude: excludeIndex >= 0 ? true : false,
showEffect: showEffectIndex >= 0 ? true : false,
};
return of(item);
})
),
of(response.accounts.model),
forkJoin(
response.rules.model.map((rule) => {
switch (rule.type) {
case NotificationType.Account: {
return this.getOneRuleItem_Account(rule).pipe(take(1));
}
case NotificationType.TxnType: {
return this.getOneRuleItem_TxnType(rule).pipe(take(1));
}
case NotificationType.Currency: {
return this.getOneRuleItem_Currency(rule).pipe(take(1));
}
default: {
return of();
}
}
})
),
]);
})
);
}

如果没有返回规则,则不需要运行下面的代码段。是否有一种方法可以像if(response.rules.length > 0)那样在这个函数中运行检查,然后只运行这段代码?

forkJoin(
response.rules.model.map((rule) => {
switch (rule.type) {
case NotificationType.Account: {
return this.getOneRuleItem_Account(rule).pipe(take(1));
}
case NotificationType.TxnType: {
return this.getOneRuleItem_TxnType(rule).pipe(take(1));
}
case NotificationType.Currency: {
return this.getOneRuleItem_Currency(rule).pipe(take(1));
}
default: {
return of();
}
}
})
),

您可以使用RxJSiif函数。你也可以使用RxJS的EMPTY常量来代替of()。这样外层的forkJoin仍然会发出,但你不会在订阅中获得undefined。当然,如果您希望在订阅中使用undefined,您可以继续使用of()

试试下面的

iif(
() => !!response.rules && !!response.rules.model,  // <-- use `forkJoin` only if `response.rules` is defined
forkJoin(
response.rules.model.map((rule) => {
switch (rule.type) {
case NotificationType.Account: {
return this.getOneRuleItem_Account(rule).pipe(take(1));
}
case NotificationType.TxnType: {
return this.getOneRuleItem_TxnType(rule).pipe(take(1));
}
case NotificationType.Currency: {
return this.getOneRuleItem_Currency(rule).pipe(take(1));
}
default: {
return EMPTY;
}
}
})
)
)

我已经包含了两个内部forkJoins的条件。

public getNotificationRuleItemsSet(): Observable <any[]> {
...
return this.getNotificationRules().pipe(
take(1),
tap((response: any) => {
if (response.txntypes) {
this.txnTypes = response.txntypes.model;
}
if (response.accounts) {
this.accounts = response.accounts.model;
}
}),
switchMap((response: any) => {
return forkJoin([
iif(
() => !!response.txntypes && !!response.txntypes.model,
forkJoin(
response.txntypes.model.map((txntype) => {
let commonIndex = commonTxnList.indexOf(txntype.txntypename);
let excludeIndex = excludeTxnTypes.indexOf(txntype.txntypename);
let showEffectIndex = showEffectForTxnTypes.indexOf(
txntype.txntypename
);
let item: INotificationTxnType = {
id: txntype.id,
txntypename: txntype.txntypename,
locname: txntype.locname,
shortList: commonIndex >= 0 ? true : false,
exclude: excludeIndex >= 0 ? true : false,
showEffect: showEffectIndex >= 0 ? true : false,
};
return of(item);
})
)
), 
of (response.accounts.model),
iif(
() => !!response.rules && !!response.rules.model,
forkJoin(
response.rules.model.map((rule) => {
switch (rule.type) {
case NotificationType.Account: {
return this.getOneRuleItem_Account(rule).pipe(take(1));
}
case NotificationType.TxnType: {
return this.getOneRuleItem_TxnType(rule).pipe(take(1));
}
case NotificationType.Currency: {
return this.getOneRuleItem_Currency(rule).pipe(take(1));
}
default: {
return EMPTY;
}
}
})
)
),
]);
})
);
}

最新更新