繁体   English   中英

为什么这个 React/Javascript object 未定义?

[英]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": {

        ....

    }

等等

它基本上只是说所有这些对象( starDescplanetDescmoonDescobjectDesc )在它们应该是新的 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.

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