繁体   English   中英

React TypeError:无法读取未定义的属性-但我可以读取其他道具

[英]React typeerror: Cannot read property of undefined - But I can read other props just fine

这个未定义的错误使我感到困惑。 这是我可以从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之外,我都可以访问和渲染所有其他道具。 有任何想法吗?

执行操作时,您的options对象未定义

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

尝试做

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM