React Promise - TypeError: Cannot read property 'then' of undefined

React Promise - TypeError: Cannot read property 'then' of undefined

我正在从以下调用中获取数据,其中 returns 一个 object:

function getPersonList() {
        const api = 'myapistring';
        axios.get(api).then(res => {
            console.log(res);
        }).catch(err => console.log(err));
}

1 - 但是当我点击我的 componentDidMount 时。诺言被打破了,我不知道为什么。

2- 此外,由于响应是一个对象,我将初始状态设置为空 [ ] 是否做错了什么? -我不确定将其设置为对象的语法是什么。

const App = React.createClass({
    getInitialState() {
        return {
            personList: [],
            visiblePersonList: []
        };
    },
    componentDidMount() {
         console.log(getPersonList(response));
        getPersonList().then((data) =>
            this.setState({
                data,
                visiblePersonList: data
            }));
        //return getPersonList;
    },
.....

谢谢大家!

您没有从 getPersonList

返回任何东西
function getPersonList() {
    const api = 'myapistring';
    return axios.get(api).then(res => {  // FIX THIS LINE WITH return
        console.log(res);
    }).catch(err => console.log(err));
}