ReactJS:无法读取未定义的属性(读取“then”)TypeError:无法读取未定义的属性

问题描述 投票:0回答:1

这是我的代码(我正在关注 YouTube 视频来调用 REST API)。它在 "then((response..." 下面

)处出现问题

**src/components/EmployeeComponent.js **

import React from "react";
import EmployeeService from "../services/EmployeeService";

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

    componentDidMount() {
        EmployeeService.getEmployees().then((response) => {
            this.setState({ employees: response.data });
        });

        //EmployeeService.getEmployees = () => {
        //  axios.get("employees").then((response) => {this.setState ( {emmployees: response.data,});
        //  } );
        //};
    }

    render() {
        return (
            <div>
                <h1 className="text-center"> Employee List</h1>
                <table className="table table-striped">
                    <thead>
                        <tr>
                            <td> Employee First Name</td>
                            <td> Employee Last Name</td>
                            <td> Manager ID</td>
                        </tr>
                    </thead>
                    <tbody>
                        {this.state.employees.map((employee) => (
                            <tr key={employee.first_name}>
                                <td> {employee.first_name}</td>
                                <td>{employee.last_name}</td>
                                <td>{employee.manager_id}</td>
                            </tr>
                        ))}
                    </tbody>
                </table>
            </div>
        );
    }
}

export default EmployeeComponent;

*现在我在 src/services/EmployeeComponent.js 中有 EmployeeService.js 。当我尝试导出时,它也在抱怨*

import axios from "axios";

const EMPLOYEES_REST_API_URL = "http://localhost:8080/";

class EmployeeService {
    getEmployees() {
        axios.get(EMPLOYEES_REST_API_URL);
    }
}

export default new EmployeeService();


我收到以下错误

未捕获的运行时错误: × 错误 无法读取未定义的属性(读取“then”) 类型错误:无法读取未定义的属性(读取“then”) 在 EmployeeComponent.componentDidMount (http://localhost:3000/main.9a0e6d67a8efe7f76fca.hot-update.js:33:85) 在 commitLayoutEffectOnFiber (http://localhost:3000/static/js/bundle.js:29607:34) 在 commitLayoutMountEffects_complete 处(http://localhost:3000/static/js/bundle.js:30774:13) 在 commitLayoutEffects_begin (http://localhost:3000/static/js/bundle.js:30763:11) 在 commitLayoutEffects (http://localhost:3000/static/js/bundle.js:30709:7) 在 commitRootImpl

我对reactJS不是很了解,很长一段时间后才开始

我已经尝试过:

import axios from "axios";

const EMPLOYEES_REST_API_URL = "http://localhost:8080/";

class EmployeeService {
    getEmployees() {
        axios.get(EMPLOYEES_REST_API_URL);
    }
}

//export default new EmployeeService();

const employeeServiceObject = new EmployeeService();

export default employeeServiceObject;
reactjs rest undefined
1个回答
0
投票

问题是你的 getEmployees() 函数没有返回任何东西。有 2 种方法可以做到这一点

1.功能简单

 getEmployees() {
     return   axios.get(EMPLOYEES_REST_API_URL);
    }

2.箭头功能

 getEmployees=()=>axios.get(EMPLOYEES_REST_API_URL);
   
© www.soinside.com 2019 - 2024. All rights reserved.