簡體   English   中英

React Promise-TypeError:無法讀取未定義的屬性“ then”

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

我正在從以下返回一個對象的調用中獲取數據:

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));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM