TypeError: ### is null,空指针导致的报错
Posted qianyy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TypeError: ### is null,空指针导致的报错相关的知识,希望对你有一定的参考价值。
开发中你无法保证后台给你的数据是不是有值的,所有前端应该抱着对后台不信任的原则,做好安全防控,不然很容易出bug报错。
比如
data.closingCustomDateType = {
key:
data.closingCustomDateType &&
data.closingCustomDateType.id || 1,
label:
data.closingCustomDateType &&
data.closingCustomDateType.name || ‘请选择时间‘,
value:
data.closingCustomDateType &&
data.closingCustomDateType.id || 1,
};
如果data是null或者underfind的时候就报错了,那我们如何来防止这样没必要的错误呢
方法一:
data.closingCustomDateType = {
key:
data &&
data.closingCustomDateType &&
data.closingCustomDateType.id || 1,
label:
data &&
data.closingCustomDateType &&
data.closingCustomDateType.name || ‘请选择时间‘,
value:
data &&
data.closingCustomDateType &&
data.closingCustomDateType.id || 1,
};
方法二(需要babel7支持):
data.closingCustomDateType = {
key:data?.closingCustomDateType?.id || 1,
label:data?.closingCustomDateType?.name || ‘请选择时间‘,
value:data?.closingCustomDateType?.id || 1,
};
好了,要是有其他的方法,欢迎留言分享
以上是关于TypeError: ### is null,空指针导致的报错的主要内容,如果未能解决你的问题,请参考以下文章