I have a child component that is reading the changes of a selection from a drop down list. I'm trying to set local variables on the component based on when the selection changes and it works unless I clear out the selection, then I get:
Cannot read properties of null (reading 'carrierStatusId')
The code:
public carrierCorporateStatusEnum = CarrierCorporateApprovalStatus;
public carrierStatusEnum = CarrierStatus;
public carrierAddendumStatusEnum = CarrierAddendumStatus;
ngOnChanges(changes: SimpleChanges) {
if (changes.carrier.currentValue != changes.carrier.previousValue )
{
this.carrier = changes.carrier.currentValue;
this.corporateStatus = changes.corporateStatus.currentValue;
this.carrierStatus = changes.carrier.currentValue.carrierStatusId;
this.carrierAddendumStatus = changes.carrier.currentValue.addendumStatus;
}
}
I know there's a way to check for null but I can't quite figure it out. Think I'm making this harder than it is.
CodePudding user response:
this.carrierStatus = changes.carrier.currentValue?.carrierStatusId;
Add the "?" before .carrierStatusId.
But take care that this.carrierStatus
will be null.