简体   繁体   English

this.props.map不是react中的函数

[英]this.props.map is not a function in react

Here is my App.js 这是我的App.js

class App extends React.Component {
  constructor(props) {
        super(props);
        this.state = {
      employees: []
    };
  }

  loadEmployees() {
    fetch('http://localhost:8080/Employee') 
    .then((response) => response.json()) 
    .then((responseData) => { 
      this.setState({ 
        employees: responseData
      }); 
    });     
} 

    componentDidMount() {
        this.loadEmployees();
    }

    render() {
        return (
            <EmployeeList employees={this.state.employees}/>
        )
    }

}

export default App;

Here is the Employee.js 这是Employee.js

class Employee extends React.Component {
    render() {
        return (
            <tr>
                <td>{this.props.employee.firstName}</td>
                <td>{this.props.employee.lastName}</td>
                <td>{this.props.employee.description}</td>
            </tr>
        )
    }
}

export default Employee;

And EmployeeList.js 和EmployeeList.js

class EmployeeList extends Component {
    render(){
        var employees = this.props.employees.map(employee =>
          <Employee key={employee._links.self.href} employee={employee}/>
        );
        return (
          <table>
            <tbody>
              <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Description</th>
              </tr>
              {employees}
            </tbody>
          </table>
        )
      }
}

export default EmployeeList;

I have spring-boot rest point at the http://localhost:8080/Employee 我在http:// localhost:8080 / Employee上有spring-boot休息点

Here is my Controller class. 这是我的Controller类。

@RestController
public class EmployeeController {

    @Autowired
    EmployeService employeeService;

    @RequestMapping(value="/Employee")
    public List<Employee> defaultEmployee() {
        return employeeService.getAllData();
    }
}

I am returning List<Employee> , it turns to JSON, when I try to catch this result, here is the error. 我返回List<Employee> ,它变为JSON,当我尝试捕获此结果时,这是错误。

The Rest returns employees from the DB, but i can not map it into the list. 其余人员从数据库返回员工,但我无法将其映射到列表中。

错误

Can anyone tell me what is the problem? 谁能告诉我这是什么问题?

Make sur that the this.state.employees is an array , the things in react the component will render no mather if the value of the props are empty or not and the map function will trow an error if you try to pass an empty array 确保this.state.employees是一个数组,如果props的值是否为空,则react组件中的内容将不进行任何运算,并且如果您尝试传递一个空数组,则map函数将引发错误。

so on your EmployeeList.js added this code 所以在您的EmployeeList.js添加了此代码

class EmployeeList extends Component {
    render(){
       var employees = <div>Loading</div>
       if(this.props.employees.length > 0){
          employees = this.props.employees.map(employee =>
          <Employee key={employee._links.self.href} employee={employee}/>
        );
         } 
        return (
          <table>
            <tbody>
              <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Description</th>
              </tr>
              {employees}
            </tbody>
          </table>
        )
      }
}

Please test it and let me know if it work 请测试一下,让我知道它是否有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM