英文:
Getting Object is Undefined Error in Angular
问题
我最近进行了Angular升级,但我遇到了一个"对象可能未定义"的错误,而我无法弄清楚如何解决它。我已经在Google上搜索到了不少解决方案,但都没有起作用。我尝试过使用"if"语句来确保对象已定义,使用null合并运算符,内联的null检查,设置属性中的问号,但问题仍然存在。我现在几乎无法继续进行开发。这个属性不应该为null。
以下是代码:
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;
};
错误信息显示signalHead可能未定义,lights也可能未定义。
以下是SignalHead的对象定义:
export interface SignalHead{
lights: Light[];
location: SignalHeadLocation;
}
如果有问题或其他需要的信息,请告诉我。
谢谢您的时间,
Shaun
英文:
I recently did an Angular upgrade and I'm getting an 'object is possibly undefined' error and I cannot figure out how to get rid of it. I've Googled this and found quite a few solutions but none of them are working. I've tried using an 'if' statement to make sure the object is defined, null coalesce, inline null check, setting the question mark in the properties and yet it remains. I'm pretty much dead in the water at this point. This property should never be null.
Here is the code:
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 is saying signalHead maybe undefined and lights maybe undefined.
Here is the object definition for SignalHead:
export interface SignalHead{
lights:Light[];
location:SignalHeadLocation;
}
If there's questions or anything else you need, let me know.
Thank you for your time,
Shaun
答案1
得分: 2
在一切都说完和完成之后,你需要考虑一件事,根据编译器的说法,你的 getDisplayClass()
函数可能会返回 undefined
;要么是因为它找不到匹配的 SignalHead
,要么是因为找不到匹配的 Light
。
处理这个问题的最简单方法是将 getDisplayClass()
的返回值更改为 string | undefined
,然后在函数的其余部分使用可选链接运算符来处理可能的 undefined
值:
function getDisplayClass(signal: Signal, signalHeadLocation: string, lightId: string): string | undefined {
const signalHead = signal.signalHeads.find(signalHead => signalHead.location.toLowerCase() == signalHeadLocation.toLowerCase());
return signalHead?.lights.find(light => light.id.toLowerCase() == lightId.toLowerCase())?.display;
};
英文:
After everything is said and done, you're going to have to account somewhere for the fact that, according to the compiler, your getDisplayClass()
function possibly returns undefined
; either because it can't find a matching SignalHead
or a matching Light
.
The easiest way to go about that is to change the return value of getDisplayClass()
to string | undefined
, and in the rest of your function use the optional chaining operator to account for the possible undefined
values:
function getDisplayClass(signal: Signal, signalHeadLocation: string, lightId: string): string | undefined {
const signalHead = signal.signalHeads.find(signalHead => signalHead.location.toLowerCase() == signalHeadLocation.toLowerCase());
return signalHead?.lights.find(light => light.id.toLowerCase() == lightId.toLowerCase())?.display;
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论