0

For this particular line of code, I am getting an error that: Object is null or undefined.

  private modalService: BsModalService

  const modalRef = this.modalService.show(ABStationDialogComponent);
  modalRef.content.presetStations = this.allStations;
  modalRef.content.editStation = JSON.parse(JSON.stringify(this.editStation)); // Deep copy
  modalRef.content.relatedFrom = this.editAbEntry.fromDate ? this.editAbEntry.fromDate : '';
  modalRef.content.relatedTo = this.editAbEntry.toDate ? this.editAbEntry.toDate : '';
  modalRef.content.editStations = this.editStations;
  modalRef.content.pageReady = true;

I tried to use Optional Chaining but did not help. then I am getting this error:

The left-hand side of an assignment expression may not be an optional property access.

Please look at the image and the error.

enter image description here

Even tried to wrap up with if statement did not help as well. I am getting this error in my angular app after installing webpack 5.

can someone please help me to solve this issue?

Kazi
  • 1,461
  • 3
  • 19
  • 47
  • what does `this.modalService.show(ABStationDialogComponent);` return? – depperm Oct 13 '21 at 10:47
  • it is a bootstrap modal service @depperm https://github.com/valor-software/ngx-bootstrap/blob/development/src/modal/bs-modal.service.ts – Kazi Oct 13 '21 at 10:50

1 Answers1

2

Let's break it down a little:

Line 276: this.modalService.show()

The method "show()" on your modalService has a no explicit return type, meaning that it can return null, undefined or any object for that matter.

If the object is null or undefined, the properties on the modalRef (like content) might not be there, that is why your IDE is marking it.

Fix

To fix this issue, you can wrap the modalRef.content in an if statement and make sure the content property is available.

instead of using a question mark (which indicates that the modalRef is optional), you can then use an exclamation mark to tell your IDE that the modalRef is always set.

if (modalRef && modalRef.content) {
  modalRef!.content.presetStations = "anything"
}
Napinator
  • 500
  • 2
  • 12