Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何从API中获取数据(当我使用map时,我获取TypeError:userdata.map不是函数)_Reactjs_Api_Axios - Fatal编程技术网

Reactjs 如何从API中获取数据(当我使用map时,我获取TypeError:userdata.map不是函数)

Reactjs 如何从API中获取数据(当我使用map时,我获取TypeError:userdata.map不是函数),reactjs,api,axios,Reactjs,Api,Axios,我需要帮助来显示API中的数据。 当我试图用map获取数据时,我得到了一个错误。TypeError:userdata.map不是函数 import React from "react"; import axios from "axios"; export class HighscoreList extends React.Component { constructor(props) { super(props); this.state = { users: ""

我需要帮助来显示API中的数据。 当我试图用map获取数据时,我得到了一个错误。TypeError:userdata.map不是函数

import React from "react";
import axios from "axios";
export class HighscoreList extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      users: ""
    };
  }
  componentDidMount() {
    axios
      .get("https://schnitzeljagdar.herokuapp.com/users/getAllUser")
      .then(response => {
        console.log(response.data);
      })
      .catch(error => {
        console.log(error);
      });
  }

  render() {
    return (
      <React.Fragment>
        <h2>User</h2>
        {this.state.users}
      </React.Fragment>
    );
  }
}
从“React”导入React;
从“axios”导入axios;
导出类HighscoreList扩展React.Component{
建造师(道具){
超级(道具);
此.state={
用户:“
};
}
componentDidMount(){
axios
.get(“https://schnitzeljagdar.herokuapp.com/users/getAllUser")
。然后(响应=>{
console.log(response.data);
})
.catch(错误=>{
console.log(错误);
});
}
render(){
返回(
使用者
{this.state.users}
);
}
}
试试这个

导出默认类HighscoreList扩展React.Component{
状态={
用户:[]
};
componentDidMount(){
axios
.get(“https://schnitzeljagdar.herokuapp.com/users/getAllUser")
.then(res=>this.setState({users:[res.data]}))
.catch(错误=>{
console.log(错误);
});
}
render(){
const{users}=this.state;
让数组=用户[0]?Object.values(用户[0]):[];
console.log(数组)
返回(
{array.map((arr,index)=>
{arr.username}
{arr.email}

)} ); } }
您可以使用一个单引号将“TypeError:userdata.map不是函数”格式化为:
TypeError:userdata.map不是函数
export default class HighscoreList extends React.Component {
    state = {
      users: []
    };

  componentDidMount() {
      axios
      .get("https://schnitzeljagdar.herokuapp.com/users/getAllUser")
      .then(res => this.setState({users:[res.data]}))
      .catch(error => {
        console.log(error);
      });
  }

  render() {
    const {users} = this.state;
    let array = users[0]?Object.values(users[0]):[];
    console.log(array)
    return (
      <React.Fragment>
         {array.map((arr,index)=>
          <div key={index}>
          <h2>{arr.username}</h2>
          <p>{arr.email}</p>
          </div>
          )}
      </React.Fragment>
    );
  }
}