简体   繁体   English

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

[英]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.

相关问题 React - TypeError:无法读取未定义的属性“道具” - React - TypeError: Cannot read property 'props' of undefined React Redux - Uncaught(in promise)TypeError:无法读取未定义的属性'props' - React Redux - Uncaught (in promise) TypeError: Cannot read property 'props' of undefined 未捕获的TypeError:无法读取react-redux中未定义的属性'props' - Uncaught TypeError: Cannot read property 'props' of undefined in react-redux React TypeError:在传递道具时无法读取未定义的属性“map” - React TypeError: Cannot read property 'map' of undefined on passing props React教程:TypeError:无法读取未定义的属性“ props” - React Tutorial: TypeError: Cannot read property 'props' of undefined TypeError:无法读取undefined的属性'firstName' - 在react中使用'props' - TypeError: Cannot read property 'firstName' of undefined - using 'props' in react React/Redux - TypeError:无法读取未定义的属性“道具” - React/Redux - TypeError: Cannot read property 'props' of undefined TypeError:无法读取 React function 中未定义的属性“道具” - TypeError: Cannot read property 'props' of undefined In a React function 反应功能组件类型错误:无法读取未定义的属性“道具” - React functional component TypeError: Cannot read property 'props' of undefined React Redux - TypeError:无法读取未定义的属性“道具” - React Redux - TypeError: Cannot read property 'props' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM