简体   繁体   English

JSON对象即使存在也返回未定义状态(NASA API和React)

[英]JSON object returning undefined even though it is there(NASA API and React)

{this.imageRenderer(item.data[0].media_type, item.links[1], item.links[0])}

When I am referencing item.links it says it is undefined, but it is right there!! 当我引用item.links时,它说它是未定义的,但是就在那里! (I am getting data from NASA API) Image: (我正在从NASA API获取数据)图片:

As you can see there is the links attribute. 如您所见,这里有links属性。 I am confused to why this is happening. 我对为什么会这样感到困惑。 Console errors: 控制台错误:

图片链接

TypeError: "item.links is undefined"

render AstronomyContainer.js:127 render AstronomyContainer.js:119 finishClassComponent13React
handleSubmit AstronomyContainer.js:28 

failed to fetch data AstronomyContainer.js:40
TypeError: item.links is undefined[Learn More] AstronomyContainer.js:127

​For some odd reason this same API call works when I type in the keyword 'Sun': 出于某些奇怪的原因,当我输入关键字'Sun'时,相同的API调用也可以使用:

Sun Image工作时

If you want to check out the whole website it is here: https://nasaopenimagelibrarysearch.firebaseapp.com/ 如果您想查看整个网站,请访问: https : //nasaopenimagelibrarysearch.firebaseapp.com/

If you type in 'Hi' suppose, it is supposed to display multiple results like when it does when I type in 'Sun' (You can also try this). 如果您键入“ Hi”,它应该显示多个结果,就像我键入“ Sun”时的结果一样(您也可以尝试此操作)。 But the image property(item.links) becomes undefined like shown above. 但是图像属性(item.links)变得不确定,如上所示。

Do not forget to check the function down below for imageRenderer. 不要忘记查看下面的imageRenderer功能。 It really helps explain a lot. 这确实有助于解释很多。

Image that item.links exists: Json image What's even more odd is that the data attribute populates the "cards" when I comment out the item.links attribute. 表示存在item.links的图像Json图像更奇怪的是,当我注释掉item.links属性时,数据属性填充了“卡片”。

UPDATE: 更新:

I ran a console.log for the links when I type in 'Hi' and it gets the links for displaying the image. 当我输入“ Hi”时,我为链接运行了console.log,它获取了用于显示图像的链接。 The only problem is, is that it is still returning undefined even though it gets the links. 唯一的问题是,即使获得链接,它仍然返回未定义。

Image: Image for getting the links 图片: 用于获取链接的图片

Function for this.imageRenderer this.imageRenderer的功能

imageRenderer(stuff, stuff1, stuff2) {
    switch(stuff) {

       case('video'):
           return (
               <iframe
                   allowFullScreen
               frameBorder="0"
               height="520"
               width="720"
               src={stuff1.href}>
           </iframe>
           )

           case('image'):
               return (
               //   <a href={hdurl} className="astronomy-image-wrapper">
               //           <img src={url} alt={title} />
               //   </a> 
               <a className="astronomy-image-wrapper">
                   <img src={stuff2.href}/>
               </a>
               )

           default:
           return(<a className="astronomy-image-wrapper">
           {/* <img src={stuff1.href} /> */}
       </a>)
   }
}

I have gone through JSON output of this URL 我已经浏览了该URL的 JSON输出

I think the problem is in this line 我认为问题在于

{this.imageRenderer(item.data[0].media_type, item.links[1], item.links[0])}

You are trying to get links at index 0 and 1 您正在尝试获取索引为0和1的链接

Now when I inspect JSON for the link, not all Items have 2 links in it. 现在,当我检查JSON的链接时,并不是所有的Item都有2个链接。 So you will get an error if you try links[1] 因此,如果您尝试links[1]则会收到错误消息links[1]

Here is a fiddle which prints links length in the console 这是一个小提琴 ,可以在控制台中打印链接的长度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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