[英]React: Uncaught TypeError: Cannot read properties of undefined (reading '0') error
It is my first time js and react.js project.这是我第一次 js 和 react.js 项目。 and I have a problem with printing out json array data.
我在打印 json 数组数据时遇到问题。
Here's my code这是我的代码
const [appData, setAppData] = useState({});
const [appdevData, setAppdevData] = useState('');
axios
.all([
.get(url1)
.get(url2)
])
.then(
axios.spread((res1, res2) => {
const data1 = res1.data;
const data2 = res2.data;
setAppData(data1);
setAppdevData(data2().results[0].developer.name);
})
}, []);
return (
<section className={styles.itemDisplaySection}>
<div className={styles.itemDisplayFlex}>
<table className={styles.itemDisplayTable}>
<tr>
<td>APP ID</td>
<td>{appData.app_id}</td>
</tr>
<tr>
<td>APP Type</td>
<td>{appData.type}</td>
</tr>
<tr>
<td>Developer</td>
<td>{appdevData.results[0].developer.name}</td>
</tr>
<tr>
<td>Release Date</td>
<td>{appData.release_date}</td>
</tr>
</table>
</div>
</section>
);
and here is my appData.json, and appdevData.json data.这是我的 appData.json 和 appdevData.json 数据。
appdata.json
{
"app_id": 1089090,
"name": "testdata",
"header_url": "https://cdn.akamai.steamstatic.com/steam/apps/1089090/header_koreana.jpg?t=1658473684",
"release_date": "2020-03-26",
"type": "game",
"basegame_id": null
}
appdevData.json
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"app_dev_id": 76,
"app": "Django-server-url",
"developer": {
"developer_id": 65,
"name": "SMILE"
}
}
]
}
but still there is print issue with results[] array.但是 results[] 数组仍然存在打印问题。 here is my error code.
这是我的错误代码。
// with appdevData.results[0].developer.name
Cannot read properties of undefined (reading '0')
I've been trying to fix with map func, and props components and still have problem with it.我一直在尝试使用 map func 和 props 组件进行修复,但仍然存在问题。
I know it's a simple array print out issue, but I can't find a way to fix this error.我知道这是一个简单的数组打印问题,但我找不到解决此错误的方法。
plz help me.请帮助我。
When setting your appdevData
change the code to ( data2
is not a function):设置
appdevData
时,将代码更改为( data2
不是函数):
axios.spread((res1, res2) => {
const data1 = res1.data;
const data2 = res2.data;
setAppData(data1);
if (!data2.results || data2.results.length == 0) return;
setAppdevData(data2.results[0].developer.name);
})
Also, you could set your default appData
state to null and add conditional rendering to prevent errors due to accessing properties during load.此外,您可以将默认
appData
state 设置为 null 并添加条件渲染以防止由于在加载期间访问属性而导致的错误。
Finally, you should display the appdevData
state instead of appdevData.results[0].developer.name
:最后,您应该显示
appdevData
state 而不是appdevData.results[0].developer.name
:
const [appData, setAppData] = useState(null);
const [appdevData, setAppdevData] = useState('');
...
if (!appData) return <>Loading data...</>
return (
<section className={styles.itemDisplaySection}>
<div className={styles.itemDisplayFlex}>
<table className={styles.itemDisplayTable}>
<tr>
<td>APP ID</td>
<td>{appData.app_id}</td>
</tr>
<tr>
<td>APP Type</td>
<td>{appData.type}</td>
</tr>
<tr>
<td>Developer</td>
<td>{appdevData}</td>
</tr>
<tr>
<td>Release Date</td>
<td>{appData.release_date}</td>
</tr>
</table>
</div>
</section>
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.