ReactJS 使用 Ionic Uncaught TypeError:无法从 axios 请求中读取未定义的属性“map”

绘制134

我有一个schedules.ts文件,我有一个功能getSchedules()

import axios from 'axios'

export default function getSchedules(){
    const config = {
        headers: { Authorization: 'Bearer ' + localStorage.getItem('bearer_token') ,
        Accept: 'application/json',
        'Content-Type': 'application/json',
        }
    };
     return axios.get('http://127.0.0.1:8000/api/auth/getSchedules',config).
      then((res)=>{
       return res.data
      }).catch((err)=>{
        console.log(err)
      })
}

在我的 Tab1 组件中,我想从我的 axios 请求中获取数据并使用 array.map 进行显示

import getSchedules from '../methods/schedules'

const Tab1: React.FC = () => {

  const [schedules, setSchedules] = React.useState([]);

  React.useEffect(() => {
    getSchedules().then(data => setSchedules(data.schedules));
  }, []);


  return (
    <IonPage>
      <IonHeader>
        <IonToolbar>
          <IonTitle>Appointments</IonTitle>
        </IonToolbar>
      </IonHeader>
      <IonContent className="ion-padding">
    <IonList>

      {schedules.map(elem => {
        return(
          <IonItem key={elem['id']}>
          <IonLabel>
            <h2>{elem['schedule_type']}</h2>
            <h3>{elem['type']}</h3>
            <h3>{elem['start_date']}</h3>
          </IonLabel>
        </IonItem> 
        )
      })}

    </IonList>
  </IonContent>
    </IonPage>
  );
};
export default Tab1;

但是当我去组件时,我得到了

Tab1.tsx:38 Uncaught TypeError: Cannot read property 'map' of undefined

我是新来的反应和这个打字稿的事情。我希望我可以v-for在 vue.js 中使用like。有人可以告诉我这是什么问题,我该如何解决?谢谢..

罗宾·齐格蒙德

事实证明,问题是data从 API 返回没有schedules属性,因此setSchedules(data.schedules)使schedules undefined. 数据作为一个整体已经是所需的数组,因此需要调用setScheduleswithdata作为参数。

也就是说,身体useEffect应该是

getSchedules().then(data => setSchedules(data));

如果您愿意,可以将其简化为

getSchedules().then(setSchedules);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Axios ReactJS-无法读取未定义的属性“ setState”

Uncaught TypeError:无法使用React读取未定义的属性“ map”

Uncaught TypeError:无法使用Sweet Alert读取未定义的属性'then'

无法使用REACTJS读取未定义的属性“地图”

Uncaught TypeError:升级reactjs后无法读取未定义的属性'bool'

Uncaught TypeError:无法读取未定义的属性“ chooseEntry”(在使用fileSystem API开发chrome扩展时)

ReactJS:TypeError:无法读取未定义的属性“ map”

TypeError:无法使用ReactJs读取未定义的属性“ map”

未捕获的TypeError:无法读取reactjs应用程序中未定义的属性'map'

TypeError:无法使用vue.js和axios读取未定义的属性“ post”

FourSquare Axios请求-ReactJs-无法读取未定义的属性“ map”

TypeError:无法读取reactjs中未定义的属性'map'

TypeError:无法读取ReactJS中未定义的属性'focus'

ReactJs-TypeError:无法读取未定义的属性“ map”

Uncaught TypeError:尝试使用React MuiAlert时无法读取未定义的属性“ main”

Axios在React中未定义,错误:Uncaught TypeError:无法读取未定义的属性“ post”

无法使用Reactjs Toastr读取未定义的属性成功

TypeError:无法使用useEffect和Axios读取未定义的属性“ get”

ReactJs:使用地图浏览状态元素,TypeError:无法读取未定义的属性“ map”?

TypeError:无法读取未定义的Reactjs的属性“ map”?

TypeError:无法读取REACTJS中未定义的属性“值”

Uncaught TypeError:无法使用数据表管道读取未定义的属性“ style”

TypeError:无法读取未定义的Reactjs的属性“ map”

Uncaught TypeError:无法读取未定义的属性“结果”-ReactJS

ReactJS Uncaught TypeError:无法读取未定义的属性“concat”

无法读取ReactJS中未定义的属性“ map”

使用 AngularJS 和 Ionic 出现错误“TypeError:无法读取未定义的属性‘email’”

在 ReactJs 中使用 map 函数时出现错误“无法读取未定义的属性‘map’”

ReactJS Uncaught TypeError:无法读取未定义的属性(读取“电子邮件”)