I just completed an upgrade to Angular and encountered an issue with the error message 'object is possibly undefined'. I have tried multiple solutions after doing some research online, including using 'if' statements, null coalescing, inline null checks, and setting question marks in properties. However, none of these methods seem to be resolving the error. This property should always have a value assigned to it.
Below is the snippet of code that is causing the error:
getDisplayClass(signal: Signal, signalHeadLocation:string, lightId:string): string {
const signalHead = signal.signalHeads.find(signalHead => signalHead.location.toLowerCase() == signalHeadLocation.toLowerCase());
return signalHead.lights.find(light => light.id.toLowerCase() == lightId.toLowerCase()).display;
};
The error indicates that signalHead or lights may be undefined. Here is the object definition for SignalHead:
export interface SignalHead{
lights:Light[];
location:SignalHeadLocation;
}
If you have any questions or require further information, please feel free to reach out.
Thank you for your assistance, Shaun