簡體   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