【发布时间】:2021-03-25 09:41:41
【问题描述】:
运行单元测试时出现错误:
TypeError: 无法读取未定义的属性“值”
在 component.ts 文件中我尝试初始化所有值:
@Input() beneficiary: string = '';
pageInfo: { startingPayment: number; endingPayment: number; total: number };
history = new BehaviorSubject<[]>([]);
history$: Observable<[]> = null;
pageNumber = 1;
maxPages: number = 0;
maxPerPage: number = 0;
totalPayments: number = 0;
prevBtnDisabled = true;
nextBtnDisabled = true;
errorMessage: string = '';
handleId: string = '';
apiCalled = true;
paginator: MatPaginator = null;
constructor(private thirdPartyAccountsStoreService: ThirdPartyAccountsStoreService) {
this.history$ = this.history.asObservable();
}
ngOnInit(): void {
this.getPaymentInitHistory();
}
getPaymentInitHistory() {
if (this.beneficiary) {
this.apiCalled = true;
this.resetProperties();
this.thirdPartyAccountsStoreService.getPaymentInitHistory(this.beneficiary).subscribe(
(result: any) => {
this.handleId = result.input.value;
this.setUpPagesProperties(result.model[0]);
this.handleResponse(result);
},
(error) => this.handleError(error)
);
}
}
在 html 中,我添加了 ngIf 以在运行 ngFor 之前检查 history$:
<div *ngIf="history$">
<div class="grid" *ngFor="let row of history$ | async">
<div class="grid-row">
<div class="text-size">{{ row.valuedate | appFormatDate | async }}</div>
<div class="secondary-text">{{ row.narrative }}</div>
</div>
<div class="grid-row-end">
<div class="text-size">
<app-amount [value]="row.credit" [currency]="row.currency"></app-amount>
</div>
<div class="secondary-text">{{ row.contranarrative }}</div>
</div>
我不确定是什么导致了这个错误。唯一使用value 的地方是在html 页面[value]="row.credit" 中,我尝试过使用[value]="row?.credit",但这也没有用。在文件this.handleId = result.input.value; 的ts 文件中,但我添加了一个检查 if (this.beneficiary),因此如果该字段为空,则不应运行函数中的代码
【问题讨论】:
-
你能添加导致错误的行吗?给定的 sn-p 不包含字段“值”的使用。
标签: angular typescript