[英]Why is this React/Javascript object undefined?
所以我試圖把這個 JSON 變成 JavaScript 對象,但我試過的所有東西都沒有定義。 對象不起作用。 我這里有一些代碼是getStaticProps
方法從我的 json 文件中獲取對象
export async function getStaticProps({ params }) {
const json = await fetch(" /* LINK */ ");
const properties = await json.json();
const { starDesc, planetDesc, moonDesc, objectDesc } = properties;
return {
props: {
starDesc,
planetDesc,
moonDesc,
objectDesc,
}
};
}
這是我的 json 文件的整體結構......
{
"starDesc": {
"starProperties": [{
"name": "Spectral Type",
"desc": "The spectral designation of a star."
},
{
"name": "Color",
"desc": "The color of a star's light. Bluer stars are usually hotter while redder stars are cooler."
},
....
]
},
"planetDesc": {
....
}
等等
它基本上只是說所有這些對象( starDesc
, planetDesc
, moonDesc
, objectDesc
)在它們應該是新的 javascript 對象時都未定義,對嗎? 我不明白我在這里做錯了什么,任何幫助將不勝感激謝謝:)
編輯:這是出口 function。 只是一個簡單的測試來顯示一些東西,但它說starDesc.starProperties
是未定義的
export default function PropertySection(props) {
return (
<p>{props.starDesc.starProperties[0].name}</p>
)
}
終於能夠讓它工作了。 我相信問題是從 object 的 html 標簽中獲得了道具,以及getStaticProps
function 中的其他道具標簽,所以我假設這些是沖突的。 我最終從包含<PropertySection>
標記的單獨頁面中獲取 JSON 數據,並從那里將 JSON 數據傳遞給它。 現在可以了。 謝謝大家的幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.