简体   繁体   English

React: Uncaught TypeError: Cannot read properties of undefined (reading '0') error

[英]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.

相关问题 反应:未捕获的类型错误:无法读取未定义的属性(读取&#39;jpg&#39;) - React: Uncaught TypeError: Cannot read properties of undefined (reading 'jpg') 未捕获的类型错误:无法读取未定义的属性(读取“地图”)反应 - Uncaught TypeError: Cannot read properties of undefined (reading 'map') React 反应:未捕获的类型错误:无法读取未定义的属性(读取“路径名”) - React : Uncaught TypeError: Cannot read properties of undefined (reading 'pathname') 反应:未捕获的类型错误:无法读取未定义的属性(读取“charAt”) - React: Uncaught TypeError: Cannot read properties of undefined (reading 'charAt') 未捕获的 TypeError:无法读取未定义的属性(读取“params”)-React - Uncaught TypeError: Cannot read properties of undefined (reading 'params') - React 未捕获的 TypeError:无法读取 Simple React 项目中未定义(读取“0”)的属性 - Uncaught TypeError: Cannot read properties of undefined (reading '0') in Simple React Project JavaScript(React)- 未捕获的类型错误:无法读取未定义的属性(读取“样式”) - JavaScript(React)- Uncaught TypeError: Cannot read properties of undefined (reading 'style') React: Uncaught TypeError: Cannot read properties of undefined (reading 'setState') - React: Uncaught TypeError: Cannot read properties of undefined (reading 'setState') 未捕获的类型错误:无法读取未定义的属性(读取“参数”)反应 - Uncaught TypeError: Cannot read properties of undefined (reading 'params') react React - Uncaught TypeError:无法读取未定义的属性(读取'setState') - React - Uncaught TypeError: Cannot read properties of undefined (reading 'setState')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM