我对这个未定义的错误感到困惑。这是我的对象,我可以从render()
中调用console.log{_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的道具:颜色和大小。即使这会导致typeError:
const color = this.props.detail.options.color || []
访问和呈现除options.color和options.size之外的所有其他道具我没有问题。有什么想法吗?
答案 0 :(得分:2)
执行
时,您的选项对象未定义const color = this.props.detail.options.color || []
尝试
const color= this.props.detail.options? this.props.detail.options.color || [] :[];