【发布时间】:2021-04-03 23:45:07
【问题描述】:
如果我在 Visual Studio 代码上单击保存,则此代码 有效。但是当我在浏览器中刷新页面(对 API 的新调用)时,它给了我这个错误:
“TypeError: 无法读取未定义的属性‘集合’”
import React from 'react'
export default function Apitest() {
const [shows, setShows] = React.useState([]);
var myHeaders = new Headers();
myHeaders.append("Authorization", "Basic xxxxxx3456789");
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
React.useEffect(() => {
fetch("https://api.vhx.tv/collections", requestOptions)
.then(response => response.text())
.then(result => setShows(JSON.parse(result)))
.catch(error => console.log('error', error));
}, [])
return (
<div>
<h1>{shows._embedded.collections[0].name} is a great show!</h1>
</div>
)
}
我认为问题在于我需要进行异步调用。
【问题讨论】:
标签: javascript reactjs api