反应类型错误:无法读取未定义的属性 - 但我可以很好地阅读其他道具



我对这个未定义的错误感到困惑。 这是我可以控制台的对象.log从 render()

{_id: "59e7ac89d14e6d644588eaff", title: "500 thread count sateen sheet set", id: "1233", description: "Sink into dreamland with a luxe sheet set in smoot…at and fitted sheets, along with two pillowcases.", type: "Duvet", …}
addedDate
:
"2017-10-18T19:33:29.468Z"
collectiontype
:
"String"
description
:
"Sink into dreamland with a luxe sheet set in smooth, lustrous organic-cotton sateen that includes flat and fitted sheets, along with two pillowcases."
holiday
:
""
id
:
"1233"
image1
:
{_id: "59e7ac89d14e6d644588eafd"}
image2
:
{_id: "59e7ac89d14e6d644588eafe"}
options
:
{color: Array(2), size: Array(5)}
price
:
(4) [89, 99, 119, 129]
season
:
"All"
subtype
:
""
tags
:
[""]
title
:
"500 thread count sateen sheet set"
type
:
"Duvet"
__v
:
0
_id
:
"59e7ac89d14e6d644588eaff"
__proto__
:
Object

在第一次渲染时,对象实际上是未定义的。 我通过将每个属性分配给一个空类型来处理这个问题。 例如:

const price = this.props.detail.price || []

出于某种原因,无论我做什么,我都无法访问选项 obj 的道具:颜色和大小。 即使这样也会导致类型错误:

const color = this.props.detail.options.color || []

我访问和渲染除 options.color 和 options.size 之外的所有其他道具都没有问题。 有什么想法吗?

当你

这样做时,你的选项对象是未定义的

const color = this.props.detail.options.color || []

尝试做

const color= this.props.detail.options? this.props.detail.options.color || [] :[];

相关内容

最新更新