[英]React typeerror: Cannot read property of undefined - But I can read other props just fine
I am puzzled by this undefined error. 这个未定义的错误使我感到困惑。 Here is my object that I can console.log from render() 这是我可以从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
On the first render the object is, in fact, undefined. 实际上,在第一个渲染中,对象是未定义的。 I handle this by assigning each property to an empty type. 我通过将每个属性分配为空类型来处理此问题。 Eg: 例如:
const price = this.props.detail.price || []
For some reason, regardless of what I do, I cannot access the props of the options obj: color and size. 由于某些原因,无论我做什么,我都无法访问obj选项的属性:颜色和大小。 Even this causes the typeError: 即使这样也会导致typeError:
const color = this.props.detail.options.color || []
I have no issue accessing and rendering all other props except options.color and options.size. 除了options.color和options.size之外,我都可以访问和渲染所有其他道具。 Any ideas? 有任何想法吗?
Your options object is undefined when you do 执行操作时,您的options对象未定义
const color = this.props.detail.options.color || []
Try doing 尝试做
const color= this.props.detail.options? this.props.detail.options.color || [] :[];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.