简体   繁体   English

JSON API 返回未定义

[英]JSON API returning undefined

When I try to show the height and weight from an API ( http://thedogapi.com/ ), I get an error:当我尝试从 API ( http://thedogapi.com/ ) 显示身高和体重时,出现错误:

TypeError: Cannot read properties of undefined (reading 'metric')类型错误:无法读取未定义的属性(读取“度量”)

If I comment the "items.height.metric" everything works great, but I have to show these results also.如果我评论“items.height.metric”,一切都很好,但我也必须显示这些结果。

From a page before I get the ID, as I click on an item from a list, that brings me to the second page, showing something like "http://localhost:3000/config/?id=2" (So, I clicked on the second item).在我获得 ID 之前的页面中,当我单击列表中的一个项目时,这会将我带到第二个页面,显示类似“http://localhost:3000/config/?id=2”的内容(因此,我单击第二项)。 GitHub: https://github.com/fvrabelo/tiroNewDogApi Any help? GitHub: https://github.com/fvrabelo/tiroNewDogApi 有什么帮助吗? love y'all爱你们

enter image description here在此处输入图片说明

 import {React} from 'react' import {useEffect, useState} from 'react'; import {Link} from 'react-router-dom' const Page = () =>{ // identifica ID na url const urlParams = new URLSearchParams(window.location.search); const id = urlParams.get('id'); // fetch da raca baseado no ID passado pela url const [items, setItems] = useState([]) useEffect(() => { const getBreeds = async () =>{ const res = await fetch( `https://api.thedogapi.com/v1/breeds/${id}` ); const data = await res.json(); setItems(data) } getBreeds() }, []) //fetch da imagem function getImage(imageId) { fetch(`https://api.thedogapi.com/v1/images/${imageId}`) .then(r =>r.json()) .then(response=> { const data = response document.querySelector(".image").src = data.url; }) } return( <div className="" style={{"display": "flex", "position":"relative", "justify-content": "center", "align-items": "center", 'flex-direction': "column", "maxWidth":"100%", "maxHeight":"100%"}}> <h6 className="card-subtitle mb-2 text-muted text-center" style={{"marginTop":"5px"}}>Breed name</h6> <h5 className="card-title text-center">{items.name}</h5> <h6 className="card-subtitle mb-2 text-muted text-center" style={{"marginTop":"5px"}}>Bred for</h6> <h5 className="card-title text-center">{items.bred_for}</h5> <h6 className="card-subtitle mb-2 text-muted text-center" style={{"marginTop":"5px"}}>Group</h6> <h5 className="card-title text-center">{items.breed_group}</h5> <h6 className="card-subtitle mb-2 text-muted text-center" style={{"marginTop":"5px"}}>Temperament</h6> <h5 className="card-title text-center">{items.temperament}</h5> <h6 className="card-subtitle mb-2 text-muted text-center" style={{"marginTop":"5px"}}>Life span</h6> <h5 className="card-title text-center">{items.life_span}</h5> <h6 className="card-subtitle mb-2 text-muted text-center" style={{"marginTop":"5px"}}>Height</h6> <h5 className="card-title text-center">{items.height.metric}</h5> <h6 className="card-subtitle mb-2 text-muted text-center" style={{"marginTop":"5px"}}>Weight</h6> <h5 className="card-title text-center">{items.weight.metric}</h5> <img className ="image" src={getImage(items.reference_image_id) } style={{ "maxHeight": "300px", "maxWidth": "300px", "marginBottom":"15px" }}/> <Link to="/" className="btn btn-primary">Home</Link> </div> ); } export default Page;
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

For now I have something like this: enter image description here现在我有这样的事情:在此处输入图像描述

This could be because the fields height.metric do not exist for some dogs (and for others they do).这可能是因为字段height.metric对于某些狗(和其他狗)不存在。 So you have to handle that possibility:所以你必须处理这种可能性:

{ items.height.metric &&
            <h6 className="card-subtitle mb-2 text-muted text-center" style={{"marginTop":"5px"}}>Height</h6>
            <h5 className="card-title text-center">{items.height.metric}</h5>
}
{ items.weight.metric &&
            <h6 className="card-subtitle mb-2 text-muted text-center" style={{"marginTop":"5px"}}>Weight</h6>
            <h5 className="card-title text-center">{items.weight.metric}</h5>
}

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

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