I am having trouble creating an ngIf statement in Angular. I am trying to show a div only when it would match a certain condition… but I keep getting this errorr in the browser console:
Cannot read properties of undefined (reading ‘0’)
What am I doing wrong?
My HTML is:
<div *ngIf="preSelectedPaymentOption[0].value === 3 && totalCartPrice > 380" class="payment-warning">
<ion-label>
<h3>Atention!</h3>
<p>Lorem ipsum dolor sit!!!.</p>
</ion-label>
</div>
This is how the data from the API looks like:
And here is the TS code how I’m getting the data:
getPaymentOptions() {
this.cartService.getPaymenyOptions().subscribe(
data => {
this.paymentData = data;
this.preSelectedPaymentOption = this.paymentData.payments.filter(option => option.default === 1);
console.log('Preselected payment method: ', this.preSelectedPaymentOption.map(option => option));
}, error => {
console.log('Error', error);
});
}
3
Answers
Your
preSelectedPaymentOption
might be undefined at the start so changing your condition to following should fix the issueThe error could occur because of the Angular life cycle hooks.
Do you get the Data before the Template is build?
Here is the angular life cycle hook documentation:
https://angular.io/guide/lifecycle-hooks
The issue seems to be the fact that the data is being fetched over the network (using a webservice, I assume) and there is no guard for the duration when the view has loaded but the value has not yet been retrieved yet.
Based on the answer by @jitender, you can check if
preSelectedPaymentOption
is not null before accessing it’s value.I would, however, recommend a cleaner full-RxJS approach as below:
It is not clear from your code if you have any other use for
preSelectedPaymentOption
other than simply checking ifpreSelectedPaymentOption[0].value === 3
.If you don’t need access to
preSelectedPaymentOption
for anything else except to check if the value === 3, then you can include that in the observable too:Note: I have not run this code, so it might contain errors. Please use your discretion.